admin管理员组文章数量:1194126
the useState hooks are great. I mainly use the useState hooks to initialise certain states, and I also pass the function to children components to change the states. However, I realise I am starting to use too many useState hooks in my parent page component. This looks and feels wrong, because I am starting to have about 6-10 useState hooks in the parent page component.
Without showing the code, Is there a better way to do this? Maybe a better practice, or a better way to refactor.
Thanks
the useState hooks are great. I mainly use the useState hooks to initialise certain states, and I also pass the function to children components to change the states. However, I realise I am starting to use too many useState hooks in my parent page component. This looks and feels wrong, because I am starting to have about 6-10 useState hooks in the parent page component.
Without showing the code, Is there a better way to do this? Maybe a better practice, or a better way to refactor.
Thanks
- 5 If your state gets frequently changed in predictable ways and the state is related (e.g., not just a bunch of inputs in a form with different values), then useReducer might be a good way to go. – Robert Moore Commented Jul 1, 2020 at 16:51
- 2 This might help. reactjs.org/docs/… – Julio de Leon Commented Jul 1, 2020 at 16:58
- 2 I agree with Robert Moore. I recently refactored a component that was bloated with individual states and it is a major improvement. Just to add to the suggested reading kentcdodds.com/blog/should-i-usestate-or-usereducer – Matt Fikowski Commented Jul 1, 2020 at 17:33
2 Answers
Reset to default 25Whenever you encounter a problem like this you should first look if you can split your component into multiple smaller ones. However there are scenarios where that's not an option. In those cases I would advice using useReducer.
// before
const {cache, setCache } = useState({});
const {posts, setPosts } = useState({});
const {loading, setLoading } = useState(false);
// Would become after refactor
const initialState = {
cache: {},
posts:{},
loading: false
}
const [state, dispatch] = useReducer(reducer, initialState);
I believe the best way of optimising useStates can be done by decoupling the unnecessary relationships. Whether you do that Inside the react component with useState, share them with useContext, useReducer or any other method. Putting everything in a single object tree does not optimise performance. So changing the above into a single useReducer merely moves the problem elsewhere.
The most optimised solution is going to be to only couple the necessary dependencies. Ie use multiple state handlers - ie multiple context providers, or multiple useReducers, zustand or do something clever with new useSignal which is essentially going to do the same thing by only listening on the minimum amount of diffs.
You can still subscribe between those multiple states if needed via a central useEffect.
A crude example with useContext:
<ProviderStateX>
<ProviderStateY>
<ProviderStateZ>
<MyComNeedsX>
<MyCompNeedsXY>
<MyCompChildNeedsY>
</>
<MyCompNeedsXZ>
<MyCompChildNeedsZ>
</>
</>
</>
</>
</>
There are tidier options without nesting Providers, but that example is for 'context' pardon the pun.
A better, cleaner solution is probably going to be using signal, multiple zustand states or native js proxies and only listening in the single thing that your react component cares about to prevent unnecessary re-render.
本文标签: javascriptUsing too many useState hooks in react How do I refactor thisStack Overflow
版权声明:本文标题:javascript - Using too many useState hooks in react. How do I refactor this? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738465412a2088274.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论