admin管理员组文章数量:1415467
With React classes when you have state in the constructor you could inherit it to the child ponent directly and from the child to the parent using callbacks. How can you transfer state from parent to child with hooks? Is the only way useReducer
or Redux?
With React classes when you have state in the constructor you could inherit it to the child ponent directly and from the child to the parent using callbacks. How can you transfer state from parent to child with hooks? Is the only way useReducer
or Redux?
- 2 Props work the same regardless of functional ponent vs class-based ponent. State values and callbacks passed as props. – Drew Reese Commented May 18, 2020 at 8:14
3 Answers
Reset to default 4The 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 ponents.
you can declare your state in parent with useState and pass it down as props to child ponent as you would normally have done with class ponents or functional ponents previously
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
Please refer the react docs with hooks FAQs
Classes and functional ponents (or func-p as my mate calls them) are the same in respect to props.
You can pass props from parent to child in a functional ponent just like how you'd do with a 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 ponent 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!
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 remend position instead (see https://reactjs/docs/position-vs-inheritance.html).
With functional ponents, position is probably the only choice, which means that your "parent" ponent would render the "child" ponent, passing whatever state it needs to pass via the child's props.
Whether your project needs Redux or not should be pletely orthogonal to the position-vs-inheritance question.
本文标签: javascriptHow to transfer state between React components with hooksStack Overflow
版权声明:本文标题:javascript - How to transfer state between React components with hooks? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745230594a2648805.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论