Skip to content
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

Day97:请讲一下react-redux的实现原理? #909

Open
Genzhen opened this issue Jul 14, 2020 · 1 comment
Open

Day97:请讲一下react-redux的实现原理? #909

Genzhen opened this issue Jul 14, 2020 · 1 comment
Labels
React teach_tag

Comments

@Genzhen
Copy link
Collaborator

Genzhen commented Jul 14, 2020

No description provided.

@Genzhen Genzhen added the React teach_tag label Jul 14, 2020
@Genzhen
Copy link
Collaborator Author

Genzhen commented Jul 14, 2020

每日一题会在下午四点在交流群集中讨论,五点 Github、交流群同步更新答案

实现原理

redux

1.Provider

Provider的作用是从最外部封装了整个应用,并向connect模块传递store

2.connect

负责连接React和Redux

1)获取state

connect通过context获取Provider中的store,通过store.getState()获取整个store tree 上所有state

2)包装原组件

将state和action通过props的方式传入到原组件内部wrapWithConnect返回一个ReactComponent对象Connect,Connect重新render外部传入的原组件WrappedComponent,并把connect中传入的mapStateToProps, mapDispatchToProps与组件上原有的props合并后,通过属性的方式传给WrappedComponent

3)监听store tree变化

connect缓存了store tree中state的状态,通过当前state状态和变更前state状态进行比较,从而确定是否调用this.setState()方法触发Connect及其子组件的重新渲染

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
React teach_tag
Projects
None yet
Development

No branches or pull requests

1 participant