'passing props to props.children in react

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.



Solution 1:[1]

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;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kanuos