3 ms·
Pretty excited by these features. Great work! I still need to go find my "aha!" moment for contexts. I get them but I haven't developed that intimacy where I k
by Waterluvian 8y ago
Pretty excited by these features. Great work!
I still need to go find my "aha!" moment for contexts. I get them but I haven't developed that intimacy where I know when I should use them.
Also I'm finding it trickier to know which method for component definition to reach for. I love that we have freedom of choice. But I struggle to pick between classes (pure and not), class methods (perhaps to clean up big render() methods or define class-relevant subcomponents), and functional components (memoized or not). I had someone getting into React asking me about it and I didn't have a really solid answer. I think I just confused them. This is probably just a tutorial/documentation/examples kind of problem.
- cstrat 8y agoI've got apps where I am passing props down a few levels (perhaps three components deep). What I don't completely understand if Context is a _better_ solution for what I am doing or if it is just another way to accomplish the same thing. Love that React is always improving!
- Waterluvian 8y agoI hear you. I usually use Redux and I find myself `connect`ing a lot more things than I used to. So far I haven't found any seriously painful reasons to do so. Like each tab of my phone app is connected. Each major "concept" in my app is connected. Generally everything above connected components are purely layout/app navigation and it seems to work. I'm betting Dan would suggest it's not necessarily a bad idea and might be fine. I suppose context is a way of doing that. But I'm just don't grok it yet. I should experiment a lot more.
- acemarke 8y agoGenerally, connecting more components to Redux is better for performance. The cost of more `mapState` calls is less than the cost of more "wasted" renders, and when a component needs smaller pieces of the Redux state, other state changing means it's not affected. See my post "Practical Redux, Part 6: Connected Lists and Performance" [0] and the Redux FAQ entry on using React-Redux [1] for more details. Also, we're completely rewriting the React-Redux docs from scratch, and they now have their own site [2] . We've got some new tutorial material available, and a guide section on writing `mapState` functions. The new section on using `mapDispatch` should be up soon, and we've got a lot more planned, including sections on writing selectors, performance optimizations, and how React-Redux works internally. [0] https://blog.isquaredsoftware.com/2017/01/practical-redux-part-6-connected-lists-forms-and-performance/ https://blog.isquaredsoftware.com/2017/01/practical-redux-pa... [1] https://redux.js.org/faq/reactredux https://redux.js.org/faq/reactredux [2] https://react-redux.js.org https://react-redux.js.org
- __s 8y agoWhere I'm at we were putting refs into redux, but since HTML elements are mutable they don't quite belong in redux. So moved them into an API where the refs can be passed down the tree with contexts. Then use those refs for rendering portals. Useful for having content be able to manage a panel elsewhere on screen
- frio 8y agoWe use it for application-wide stuff that would need to be drilled down several layers, and isn't state. So -- as a contrived example -- if we have a form that submits to a configurable URL (configurable at build time), then we wrap that URL up in a context. The eventual FormComponent will then consume the URL from context, rather than being drilled through n-layers of React components as a prop. Redux isn't the right answer here, because that URL's never going to change, but it used to do the job in a pinch. Other examples are react-redux itself (which uses the legacy context API) or react-intl (which uses the legacy context API), or a themeable widget set. You put a wrapping context at the top of your tree like <Provider>, <IntlProvider messages={someMessages} locale={this.state.locale}> or <ThemeProvider theme={myEmployersColours}>, and then nested deeply in the application you use a <Message> that will look at the context provided by the IntlProvider, or the context provided by the theme, to pull out the actual message/colors it should show. Basically, context is useful for encapsulating cross-cutting concerns :).
- Waterluvian 8y agoAha. Okay I think I'm having a moment. I could possibly use this to pass a reference to a websocket stream for streaming data that's way too high frequency to be handled with redux actions. Say, 100 robots driving around, displayed by a map component. I want to handle the addition of removal of a robot as an action but the actual 10hz pose updates can just be handled by the canvas and ignored by react entirely. Maybe even context for camera streams too. Previously I would just handle a websocket stream as a global. Yuck.
- acemarke 8y agoNote that context updates do require that React walk over the component tree to find the appropriate consumers, and that itself has a cost. In fact, at the moment, it seems to be _relatively_ expensive, per the discussion in this issue : https://github.com/facebook/react/issues/13739 https://github.com/facebook/react/issues/13739 . We're actually working on converting React-Redux to use `createContext` in version 6. I had hoped that it would be a big performance improvement, but unfortunately our (admittedly limited) perf benchmarks indicate that the experimental v6 branches are slower than our current v5 release build. In v5, the individual connected components subscribe to the Redux store, and can bail out of calling `setState()` if no update is necessary. In v6, we've only got one subscription (in `<Provider>`), but any Redux store update requires that React walk the tree and update the context consumers so they can run `mapState`. So, React _always_ gets involved, whereas before we might skip React if nothing had changed. Per that issue, I'm hopeful that the React team can optimize deep context updates in the near future.