Hacker News new | past | comments | ask | show | jobs | submit login

Context to me seems like a sophisticated way of declaring global variables. Might as well do const GLOBALS = { ... } instead of another complex API that has to be loaded.



It's not, though, any more than dependency injection is "declaring global variables".


The new context API effectively makes variables available globally, but you're still using a separate (explicitly declared) Consumer for each, so you don't have any of the basic issues that go with using a single set of globals.

For a very simple example:

    import { UserIdConsumer } from '@/contexts'

    const ThisComponent = () => (
      <div>
        User ID: <UserIdConsumer>{value => value}</UserIdConsumer>
      </div>
    )

    export default ThisComponent


Not sure I follow. Where does value come from?


UserIdConsumer will call the children prop as function, UserIdConsumer could look like this:

    const UserIdConsumer props => <Fragment>props.children(theValueYouWantAvailable)</Fragment>




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: