最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Redux #subscribe vs. #mapStateToProps - Stack Overflow

programmeradmin3浏览0评论

What is the purpose of using subscribe in code like store.subscribe(render)? Doesn't using mapStateToProps and connect already cause the connected ponent to re-render each time the store's state changes?

What is the purpose of using subscribe in code like store.subscribe(render)? Doesn't using mapStateToProps and connect already cause the connected ponent to re-render each time the store's state changes?

Share Improve this question asked Jan 31, 2017 at 16:58 stackjleistackjlei 10k19 gold badges73 silver badges124 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 13

Quoting the Redux.js page:

Technically you could write the container ponents by hand using store.subscribe(). We don't advise you to do this because React Redux makes many performance optimizations that are hard to do by hand. For this reason, rather than write container ponents, we will generate them using the connect() function provided by React Redux,

If you're already using react with redux, you might as well use their package, since it is already optimized.

Also referencing a previous answer: https://stackoverflow./a/36214059/3938413

The examples showing store.subscribe(render) are using small code snippets showing you the basic way to hook redux into react.

store.subscribe es from the redux package. connect es from the react-redux package and is not a part of redux itself.

The react-redux package uses store.subscribe to hook itself into the redux dispatch cycle. You can then access the redux store by using the connect ponent enhancer.

Third party libs (or middleware for example) can hook into the store lifecycle via store.subscribe.

发布评论

评论列表(0)

  1. 暂无评论