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

Arv*_*nmo 5 components state reactjs redux next.js

return\n <div>\n    <RadioOptions />\n    <GenButton />\n    <OutputPass />\n </div>\n
Run Code Online (Sandbox Code Playgroud)\n

我最近在做我的业余项目,我发现我的代码存在问题。在Radiooptions\xc2\xa0中我使用了一些useState钩子,我想将该状态从Radiooptions组件传递到其父组件(passGen),然后从那里将该状态传递给Get Button组件以检查某些条件,如果没有问题,则生成密码。(我还将附上我的 GitHub 存储库,以便更好地访问我的源代码)

\n

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

\n

Aym*_*dps 11

在 React 中,您无法将 props 从子级传递到父级,这只是一种方式(从父级到子级)。

您应该:

  • 将状态放入父组件中,并通过在 props 中传递 setter 函数从子组件操作它
  • 使用 Redux Toolkit 之类的工具创建所有组件都可以访问的全局状态