简体   繁体   English

我可以在使用 Redux 或 Redux 存储时在组件中使用状态吗?

[英]Can I use state in components while using Redux or Redux store?

**Is it OK to Using a component state for just to handling form data, while the main data and other stuff of the application will be stored in the Redux store? **是否可以使用组件状态来处理表单数据,而应用程序的主要数据和其他内容将存储在 Redux 存储中?

I don't want to use redux-form instead manually dispatch it to the reducers**我不想使用 redux-form 而是手动将其分派给减速器**

Redux comes into the picture when we want to manage the state across different component.当我们想要管理不同组件的状态时, Redux就出现了。 It provides a centralize store where state of all the components are managed.它提供了一个集中存储,其中管理所有组件的状态。

When talking about forms, in most cases form data is only limited to the component is which is being used.在谈论表单时,在大多数情况下,表单数据仅限于正在使用的组件。 So, in that type of scenario there is no need to link them to centralized store using redux .因此,在这种情况下,无需使用redux将它们链接到集中式存储。 A simple way is to store them in the component state and get it processed.一种简单的方法是将它们存储在组件状态中并对其进行处理。 Afterwards, you can store the output of that in redux store according to your needs.之后,您可以根据需要将其输出存储在redux store 中。

It is okay to do that.这样做没问题。 You can keep a local state in the component.您可以在组件中保留本地状态。 state = {} in class components, const [localState, setLocalState] = useState() for functional components. state = {}在类组件中, const [localState, setLocalState] = useState()用于功能组件。

Or you can use custom hooks for handling your local state needs.或者您可以使用自定义钩子来处理您的本地状态需求。 Check out useInput or useForm custom hooks that are developed by the community.查看由社区开发的useInputuseForm自定义挂钩。

Yes of course.是的当然。 Some things go best in component state, some go best in a parent component, some go in Context, and some go in Redux.有些东西在组件状态下效果最好,有些在父组件中效果最好,有些在 Context 中,有些在 Redux 中。 There is little use in putting everything in Redux, it just causes a lot of boilerplate and every component tries to update when the store changes.将所有内容都放在 Redux 中几乎没有用,它只会导致大量样板文件,并且每个组件都会在存储更改时尝试更新。

You put things in Redux when different components that are at different places in the HTML tree use the same data, and it changes during the app's lifetime (things like the user's language don't change often and go in Context).当位于 HTML 树中不同位置的不同组件使用相同数据时,您将内容放入 Redux,并且它在应用程序的生命周期中发生变化(例如用户语言不会经常变化并进入 Context)。 You use it when you want to store your app's state to restore it later (which exact menus are currently open or closed is usually not that important) and when you want to keep track of state changing using the Redux dev tools.当您想要存储应用程序的状态以在以后恢复它时(当前打开或关闭的确切菜单通常并不那么重要)以及当您想要使用 Redux 开发工具跟踪状态更改时,您可以使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM