美文网首页
Problems of Context in Redux

Problems of Context in Redux

作者: Aooren | 来源:发表于2016-08-02 15:04 被阅读0次

Days ago here' a problem appeared when I use ` React context ' to finish some needs with ' Redux '.

React-Redux will cut off the link of the context between components.

At first , I need a state downs from father components to child components, it was worked very well , then when I finish the components work and turn to build containers with the library redux , my components working just now is like dead components.

And I check the context, the state was really in the components. It seems that I have any typing errors.

I just dizzy . because the Redux states come to components also by the context, but why my state can't.

** As customary, I add a function to log the state when the components update.
it works!
Remove the log function. it can't work.
This phenomenon made me associate the quantum mechanics . if being observed , it will work . **

Then I check the state, know that the state will not be updated if the components without any function touch it .

So ... Why is that?

** There just one possibility will be here which the state was optimized by the componentShouldUpdate . **

I think I know the problem really is .
the component connect comes from react-redux must have optimized the state by the componentShouldUpdate .
So I need to write a componentconnect without optimization.

Thus , I try to read the react-redux source code .
... after 1 minute ..I found out the solution .

export default connect(mapStateToProps, null, null, { pure: false })(Component);

I just need to add an object with a pure state to the connect.

Then everything has done.
Dan Abramov is my idol.

相关文章

网友评论

      本文标题:Problems of Context in Redux

      本文链接:https://www.haomeiwen.com/subject/peifsttx.html