Skip to content

Instantly share code, notes, and snippets.

@developit
Last active June 5, 2018 14:21
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save developit/5d879edb820228224dc9 to your computer and use it in GitHub Desktop.
Save developit/5d879edb820228224dc9 to your computer and use it in GitHub Desktop.
// - yes, this is the entire implementation
// - no, it doesn't actually depend on preact
export class Provider {
getChildContext () {
let { children, ...context } = this.props;
return context;
}
render ({ children }) {
return children && children[0] || null
}
}
@midudev
Copy link

midudev commented Feb 14, 2017

Good workg. :) Problem is the children prop will be passed as context as well.

export class Provider {
  getChildContext () {
    const contextsToProvide = { ...this.props }
    // remove not needed children prop for context
    delete contextsToProvide.children
    // all props are made available as context
    return { ...contextsToProvide }
  }
  render ({ children }) {
    return children && children[0] || null
  }
}

@KrofDrakula
Copy link

KrofDrakula commented Feb 21, 2017

Instead of mutation, you could use destructuring to filter out any values you don't want without having to mutate:

export class Provider {
  getChildContext () {
    const { children, ...context } = this.props;
    return context;
  }
  render ({ children }) {
    return children && children[0] || null
  }
}

@developit
Copy link
Author

Amended!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment