admin管理员组文章数量:1335599
Managing state with React only
I understand that if you're creating an application using React only, you will end up managing all of your state within different React ponents you create.
Managing state with React and Redux
If you decide to use Redux in bination with React, you can then move all of the state from each of your React ponents into the overall Redux application state. Each ponent that requires a slice of the Redux application state can then hook into the state via React-Redux's connect
function.
Question
Does this mean that you no longer need to write any React ponents that deal with React's state
(i.e. this.setState
) since React-Redux is connect
ing the React ponents with Redux state by passing data into the container
ponent as props
?
Managing state with React only
I understand that if you're creating an application using React only, you will end up managing all of your state within different React ponents you create.
Managing state with React and Redux
If you decide to use Redux in bination with React, you can then move all of the state from each of your React ponents into the overall Redux application state. Each ponent that requires a slice of the Redux application state can then hook into the state via React-Redux's connect
function.
Question
Does this mean that you no longer need to write any React ponents that deal with React's state
(i.e. this.setState
) since React-Redux is connect
ing the React ponents with Redux state by passing data into the container
ponent as props
?
- 2 Yes. Whether you should or not requires more discussion and specifics about your app. It's always nice to have a single source of truth though. – azium Commented Mar 24, 2016 at 20:04
- 1 Still when you want to stick to React only, but are searching for a more lightweight approach to handle ponent state, you could add a state management library like MobX (mobxjs.github.io/mobx). It can help you with the internal ponent state (medium./@mweststrate/…) but also store the state outside of your ponent. – Robin Wieruch Commented Aug 11, 2016 at 9:14
1 Answer
Reset to default 8There are different opinions on this, but the general view seems to be that redux should only contain "application state". Individual react ponents like dropdowns or modals will still have their own state.
There is still a lot of debate on this though, check out this issue for example about how to manage local ponent state: https://github./reactjs/redux/issues/159
Some projects have been popping up that are trying to solve this "problem":
- redux-react-local
- repose withReducer (more for reducer semantics for local, not global state)
本文标签: javascriptNo need for state in React components if using Redux and ReactReduxStack Overflow
版权声明:本文标题:javascript - No need for state in React components if using Redux and React-Redux? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742386289a2465100.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论