如何使用 React hooks + Redux [英] How to use React hooks + Redux
本文介绍了如何使用 React hooks + Redux的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是将 Redux store 与 hooks 连接起来的最佳方式吗?
我正在使用连接到 Redux 的 React 钩子制作一个简单的待办事项应用程序.我创建的模式有效,但我想知道我是否做得对,这种方法是否有问题,是否应该使用不同的模式?
App.jsxconst [initialTodos, updateTodos] = useState(store.getState());const cleanup = store.subscribe(() => updateTodos(store.getState()));useEffect(() => {返回 () =>清理();});
<块引用>
调度发生在其他组件 + todo 应用程序工作
感谢您的任何意见
解决方案
事实证明最好使用 'react-redux' connect
.
const mapStateToProps = state =>({状态:状态})连接(mapStateToProps)(组件)
因为 connect 会自动处理 store.subscribe 等
所以把钩子放在一起:)
Is this the best way to connect Redux store with hooks?
Hi I am making a simple todo app using React hooks connected to Redux. The pattern I created works but I wonder if I'm doing it right, is there something wrong with this approach, is there a different pattern one should use?
App.jsx
const [initialTodos, updateTodos] = useState(store.getState());
const cleanup = store.subscribe(() => updateTodos(store.getState()));
useEffect(() => {
return () => cleanup();
});
dispatching happens in other components + the todo app works
Thank you for any input
解决方案
It turns out it is better too use 'react-redux' connect
.
const mapStateToProps = state => ({state: state})
connect(mapStateToProps)(Component)
since connect automagically takes care of store.subscribe etc.
So drop hooks all together :)
这篇关于如何使用 React hooks + Redux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文