简体   繁体   English

在反应中将道具从子组件传递给父组件

[英]passing props form child to parent component in react

return
 <div>
    <RadioOptions />
    <GenButton />
    <OutputPass />
 </div>

I recently worked on my side-project and I found an issue in my code.我最近在我的副项目上工作,我在我的代码中发现了一个问题。 inRadiooptions I used some useState hooks, and I want to pass that state from Radiooptions component to its parent(passGen) and then from there, pass that state toGet Button component to check some condition and if nothings wrong, generate the password.在Radiooptions 我使用了一些useState 钩子,我想将该状态从Radiooptions 组件传递给它的父级(passGen),然后从那里将该状态传递给Get Button 组件以检查某些条件,如果没有错误,则生成密码。 (I'll also attach my GitHub repo for better access to my source code) (我还将附上我的 GitHub 存储库,以便更好地访问我的源代码)

https://github.com/arviinmo/palora/tree/main/components/passwordgenerator https://github.com/arviinmo/palora/tree/main/components/passwordgenerator

You can't pass props from child to parent in React, it's only one way (from parent to child).你不能在 React 中将道具从孩子传递给父母,这只是一种方式(从父母到孩子)。

You should either:您应该:

  • Put the state in the parent component and manipulate it from the child component by passing the setter function in the props将状态放在父组件中,并通过在 props 中传递 setter 函数从子组件中对其进行操作
  • Use something like Redux Toolkit to create a global state that all components can have access to使用 Redux Toolkit 之类的东西来创建所有组件都可以访问的全局状态

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

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