Difference between `Provider` and `connect` in Redux for React

ぃ、小莉子 提交于 2021-01-27 18:12:15

问题


I see that with Redux there's two ways of passing down the state to the components in a React app, one is throw the Provider component and the other one with the connect functionality. However, I wonder why there's two different ways and which one is better in performance than the other.


回答1:


They are not mutually exclusive. Actually you will almost always need to use Provider, even with connect (See: https://github.com/reactjs/react-redux/blob/master/docs/api.md#provider-store)

Without connect you would have to manually pass the store to components that need them, with nested components this gets messy, as you need to pass it down multiple levels, even through components that do not need the store if its children need the store.




回答2:


Provider(component): Attaches app to store.Utilized at applications root and wraps entire application.

Connect (function): Creates container components.



来源:https://stackoverflow.com/questions/41634942/difference-between-provider-and-connect-in-redux-for-react

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!