React Context 和 useState 一样,在处理组件时,每个组件都被单独处理,

huangapple go评论71阅读模式
英文:

Do React Context like useState , In the treatment of the components, each one is treated separately ,

问题

I have a form component and i need to use react context to manage and validate each input seperetly because i build an input component, and i build a component contain information from the form component , can react context treat each input individually or not , or use redux

React Context是否像useState一样,在处理组件时能够分别处理每个组件,或者需要使用redux?

英文:

I have a form component and i need to use react context to manage and validate each input seperetly because i build an input component, and i build a component contain information from the form component , can react context treat each input individually or not , or use redux

Do React Context like useState , In the treatment of the components, each one is treated separately or not

答案1

得分: 1

是的,您可以使用React Context来单独管理和验证表单组件中的每个输入。React Context允许您在多个组件之间共享数据和状态,而无需手动通过组件树的每个级别传递props。

为了单独处理每个输入,您可以为每个输入字段创建一个单独的上下文。每个输入组件可以消耗其相应的上下文来管理其状态并处理验证。

英文:

Yes, you can use React Context to manage and validate each input separately in your form component. React Context allows you to share data and state across multiple components without having to pass props manually through each level of the component tree.

To treat each input individually, you can create a separate context for each input field. Each input component can consume its respective context to manage its state and handle validation

答案2

得分: 0

React上下文实际上通过多层次传递属性来传递数据,而不需要实现这一点。因此,在您的情况下,就好像您将整个状态传递给每个子组件,在那里使用它。

更好的做法是直接将每个子组件需要的数据作为属性传递给它。

英文:

React context is in fact passing data with props through multiple level but without the need of implementing this. so in your case it is like you pass the whole state in which you store the inputs to each child component and use it there.

better is to pass to each child component directly as props the data it needs.

huangapple
  • 本文由 发表于 2023年7月6日 17:42:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76627505.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定