-
Notifications
You must be signed in to change notification settings - Fork 47.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cases where hooks don't currently provide a good answer vs HOC #14020
Comments
If you check the docs for React.memo closely, you can see, there is a second argument which allows doing a similar optimization that is possible with Honestly, if you really want to optimize your renders, you can have a look at MobX. It already gives you much more granular control over what should rerender without any explicit checks in shouldComponentUpdate. Redux will never be that good, be sure of that. |
Yes, I actually discussed this exact issue with Dan and Sophie at ReactConf. To explain in a bit more detail, let's say I have the following setup: // In some ancestor component. Ignore the inline object for this example.
<MyContext.Provider value={ {data: someLargeValue } } />
// Further down in the component
function SomeComponent() {
const {data} = useContext(MyContext);
const derivedData = deriveSomeData(data);
// render output based on derived data
} In this example, React-Redux is indeed a more concrete example. For version 6, we're going to be putting the entire Redux store state into context, but a given component would only want to re-render when the value extracted by its associated Dan and Sophie are aware of this, and want to add a way for functional components to bail out of rendering (ie, similar to Once that is fixed, and 16.7 goes final, we would be able to ship a |
Please try and post all feedback/questions in the Hooks RFC so that the discussion doesn't get fragmented. Thanks! |
(unsure if this is the right place, so trying it out)
I've noticed that the new React Hooks feature is aiming at providing an alternative composition pattern to HOC and render functions, but I believe that many of the use cases solved by HOC (at the framework level) cannot currently be addressed by the new hooks API.
Specifically, there is not way to incorporate React Hooks with React.memo. Unless I am incorrect, this means that any system that would like to implement optimisations based on external context, such as the react-redux
connect
function (that usesmapStateToProps
to implement an efficientshouldComponentUpdate
) will still need to rely on a HOC/render-prop to automate this optimisation.The reason I am bringing this up is because one of the main benefits stated in the documentation is to reduce framework level use of HOC that "pollute" the tree, of which the react-redux connect HOC is probably the most prevalent use case.
Additionally redux (and
useRedux
) are specifically brought up as an exemplary use case, although with the current system it will cause large optimisation issues (since with no optimisedshouldComponentUpdate
, every "connected" component will re-render on every state change).(Although this might fit into the documentation repo, this is a discussion / opinion and I do not feel it is a "mistake" that I should report, but rather a discussion on importance).
An example solution for this could be if there was a way to use contexts in
React.memo
(which unless I'm incorrect only have access toprops
andprevProps
)The text was updated successfully, but these errors were encountered: