Using React Hooks for global Redux stores

栏目: IT技术 · 发布时间: 6年前

内容简介:When hooks came out there were various articles suggesting Redux might no longer be needed, and ripostes like these which argue (rightly) thatThen as a counter-counter argument some suggested usingHowever this method has a significant drawback to performan

When hooks came out there were various articles suggesting Redux might no longer be needed, and ripostes like these which argue (rightly) that useReducer is not a replacement of Redux as it’s cumbersome for a global store:

Then as a counter-counter argument some suggested using React Context as a solution to share the state and dispatcher throughout the app:

However this method has a significant drawback to performance — as per Caveats in the React docs, whenever a new object reference is passed into <Provider value={..}> , all consumers will be re-rendered. This is different to react-redux’s connect() function which will re-render the component when the parts of the state you’re interested in change.

Today I needed a global store when using Gatsby as it completely unmounts the React tree on navigation changes, thus losing any state created by components. Unfortunately their solution for this, at least by itself, remains awkward for passing state down to deeper components and dispatches back up.

Making a global redux store with hooks

It turns out making a store with hooks is quite simple. It consists of two components:

1. The class which holds store state (redux does this with a function called createStore() but same difference)

2. A hook to receive parts of the store the component is interested in, as well as the dispatch function to make updates

Let’s look at the class first:

Simple right? How we use this will become apparent when we put it into practice. You might notice we even dispatch actions to Redux Devtools :)

Once we’ve created our store we need to be able to access it in any component, that’s where the hook comes in:

The hook:

1. Takes a part of the existing store state and assigns it to state

2. On mount adds a listener for store changes, and on unmount removes it

3. When the state changes it checks if the part that we’re interested in has changed. If it has then we update our state with `setState` which causes the component to re-render.

And finally the app code

Let’s look at a scenario where we want a light and dark theme.

Great we’ve created our store and reducer. Now let’s look at how we can use the hook to access the theme and change it.

And all that running together:

We can tersely access parts of any global store anywhere in the app and our component will only re-render if those parts of the store changed :tada:


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

未来是湿的

未来是湿的

[美] 克莱·舍基 / 胡泳、沈满琳 / 中国人民大学出版社 / 2009-5 / 39.80

一位妇女丢掉了手机,但征召了一群志愿者将其从盗窃者手中夺回。一个旅客在乘坐飞机时领受恶劣服务,她通过自己的博客发动了一场全民运动。在伦敦地铁爆炸案和印度洋海啸中,公民们用可拍照手机提供了比摄影记者更完备的记录。世界上最大的百科全书是由管理甚少的参与者们撰写的…… 不论在何处,你都能看见人们走到一起彼此分享,共同工作,或是发起某种公共行动。一部集众人之力的百科全书、一个丢失手机的传奇,这些事情......一起来看看 《未来是湿的》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具