将道具传递给道具。

发布于 2025-01-29 20:49:31 字数 469 浏览 3 评论 0原文

const Component1 = (props) => {
    const [myState, setMyState] = useState('hello world');
    const ChildComponent = props.children.type;
        return (
        <div>
              <ChildComponent data={myState} />
        </div>
            )
    }
    export default Component1;

我想要从component1到props.children的通过道具,我想问我的方法是正确的吗? ; component1 /&gt; < /code>我不想从应用程序组件中通过props我希望从component1传递给props.children。

const Component1 = (props) => {
    const [myState, setMyState] = useState('hello world');
    const ChildComponent = props.children.type;
        return (
        <div>
              <ChildComponent data={myState} />
        </div>
            )
    }
    export default Component1;

i want pass props from Component1 to props.children, i want to ask my this approach is right ?, i am reusing Component1 in my code and passing components to Component1 like this <Component1><Component2 /><Component1 /> i want not pass props from App component i want pass props from Component1 to props.children.

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

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

发布评论

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

评论(1

半寸时光 2025-02-05 20:49:31

这是一种非常复杂的方法。根据经验,您应该在单独的文件中具有单独的组件。

我不清楚您要编码的内容,但根据我的推论,这就是我的处理方式。

我将拥有单独的&lt; childComponent /&gt; < /code>和&lt; component1 /&gt; < /code>。

&lt; component1/&gt;中,我将渲染&lt; childcomponent/&gt;

import ChildComponent from "components/ChildComponent";

const Component1 = (props) => {
const [myState, setMyState] = useState('hello world');
    return <ChildComponent data={myState} />
}
export default Component1;

您可以清楚地看到component1 是一个冗余组件。

但是,如果&lt; component1/&gt;用作事件

    import ChildComponent from "components/ChildComponent";
    import Component1 from "components/Component1";
    
    const Wrapper = (props) => {
    const [myState, setMyState] = useState('hello world');
        return <Component1>
<ChildComponent data={myState} />
</Component1>
    }
        export default Wrapper;

That's a pretty convoluted approach. As a rule of thumb, you should have separate components in separate files.

I am not clear about what you are trying to code, but from what I deduced, here's how I'd approach it.

I will have separate <ChildComponent /> and <Component1 />.

Inside the <Component1 />, I will render the <ChildComponent />

import ChildComponent from "components/ChildComponent";

const Component1 = (props) => {
const [myState, setMyState] = useState('hello world');
    return <ChildComponent data={myState} />
}
export default Component1;

You can see clearly that Component1 is a redundant component.

However, if <Component1/> acts as a HOC, you can wrap the ChildComponent inside of it as

    import ChildComponent from "components/ChildComponent";
    import Component1 from "components/Component1";
    
    const Wrapper = (props) => {
    const [myState, setMyState] = useState('hello world');
        return <Component1>
<ChildComponent data={myState} />
</Component1>
    }
        export default Wrapper;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文