简体   繁体   English

state如何用hooks在React组件之间传递?

[英]How to transfer state between React components with hooks?

With React classes when you have state in the constructor you could inherit it to the child component directly and from the child to the parent using callbacks.使用 React 类,当构造函数中有 state 时,您可以将其直接继承到子组件,并使用回调从子组件继承到父组件。 How can you transfer state from parent to child with hooks?您如何使用钩子将 state 从父母转移到孩子? Is the only way useReducer or Redux?唯一的方法是使用useReducer还是Redux?

The concepts of passing props down to child or conveying information from child to parent hasn't changed with the arrival of hooks.将道具传递给孩子或将信息从孩子传递给父母的概念并没有随着钩子的到来而改变。

Hooks provide, you a way to use lifecycle like functionality and states with functional components. Hooks 为您提供了一种使用生命周期的方法,例如功能组件和状态。

you can declare your state in parent with useState and pass it down as props to child component as you would normally have done with class components or functional components previously您可以使用 useState 在父级中声明您的 state 并将其作为道具传递给子组件,就像您之前通常对 class 组件或功能组件所做的那样

For example:例如:

const Parent =() => {
  const [count, setCount] = useState(0);
  return <Child count={count} setCount={setCount} />
}

const Child = ({count, setCount}) => {
  const updateCount = () => {
     setCount(prev=> prev + 1);
  }
  return (
      <div>
         <div>Count: {count}</div>
         <button type="button" onClick={updateCount}>Increment</button>
      </div>
}

You can refer this post for more details on lifecycles with hooks:您可以参考这篇文章以了解有关带钩子的生命周期的更多详细信息:

ReactJS lifecycle method inside a function Component function 组件内的 ReactJS 生命周期方法

Please refer the react docs with hooks FAQs请参考 react docs with hooks FAQs

Perhaps, you should ask yourself why you would like to use inheritance. It seems like for many cases where many developers tend to immediately think about using OOP-style inheritance, React.js might recommend composition instead (see https://reactjs.org/docs/composition-vs-inheritance.html ).也许,你应该问问自己为什么要使用 inheritance。似乎在许多情况下,许多开发人员倾向于立即考虑使用 OOP 风格的 inheritance,React.js 可能会推荐组合(参见https://reactjs.org) /docs/composition-vs-inheritance.html )。

With functional components, composition is probably the only choice, which means that your "parent" component would render the "child" component, passing whatever state it needs to pass via the child's props.对于功能组件,组合可能是唯一的选择,这意味着您的“父”组件将呈现“子”组件,传递它需要通过子道具传递的任何 state。

Whether your project needs Redux or not should be completely orthogonal to the composition-vs-inheritance question.您的项目是否需要 Redux 应该与组合与继承问题完全正交。

Classes and functional components (or func-comp as my mate calls them) are the same in respect to props.类和功能组件(或我朋友所说的 func-comp)在道具方面是相同的。

You can pass props from parent to child in a functional component just like how you'd do with a class.您可以在功能组件中将属性从父级传递给子级,就像您处理 class 一样。


//Parent

const Parent = () => {

const [state, setState] = React.useState({ products: 1, isAvailable: true})

const addProduct = (data) => {
// Your function
}

return (
<Child product info={state} addProduct={addProduct} />

)

}


export default Parent


And in the child component you can receive the props typically the way you would will classes.在子组件中,您通常可以像上课一样接收道具。


const Child = ({productInfo, addProduct}) => {
 // Do what ever you like with the props
}


Cheers!干杯!

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

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