将道具通过react中的父母成分形成父母

发布于 2025-02-14 01:10:41 字数 540 浏览 2 评论 0原文

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

我最近从事侧面项目,在代码中发现了一个问题。 inradioptions&nbsp;我使用了一些usestate钩子,我想将该状态从RadioOptions组件传递给其父(PassGen),然后从那里传递该状态,通过该状态toget按钮组件检查某些条件,如果没有错误的话,请生成密码。 (我还将附加我的github存储库以更好地访问我的源代码)

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

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. (I'll also attach my GitHub repo for better access to my source code)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸里的快感 2025-02-21 01:10:41

您不能将道具从孩子传递给父母,这只是一种方式(从父母到孩子)。

您都应该:

  • 将状态放在父组件中,并通过在道具中传递固定器功能
  • 使用redux工具包来创建一个全局状态,从而从子组件中操纵它。

You can't pass props from child to parent in React, it's only one way (from parent to child).

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
  • Use something like Redux Toolkit to create a global state that all components can have access to
忘东忘西忘不掉你 2025-02-21 01:10:41

对于新来者,这样做是这样做的:

父零件:

import Child from './Child';
export default function Parent() {
        const data_from_child = (data) => {
        console.log(data); // or set the data to a state
    }

    return (
        <Child setter={data_from_child} /> //pass the function to the child
    )
}

子组件:

export default function Child ({setter}) {
    setter('Data from Child');
    return (<></>)
}

For the newcomers here do it so:

Parent component:

import Child from './Child';
export default function Parent() {
        const data_from_child = (data) => {
        console.log(data); // or set the data to a state
    }

    return (
        <Child setter={data_from_child} /> //pass the function to the child
    )
}

Child component:

export default function Child ({setter}) {
    setter('Data from Child');
    return (<></>)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文