0

I have a React component that is declared in the arrow-function notation. This component has a child component that is supposed to change the state of the ParentComponent. How can I achieve that? And how would it look like if we passed the change of the state as a function (changeState in this case)?

const ParentComponent = () => {
    const [myState, setMyState] = useState(false);
    const changeState = () => setMyState(!myState);

    return(
        <ChildComponent /> // pass the state variables
    );
}

const ChildComponent = () => { // receive the state variables
    // change state of parent component
}
5
  • Pass props to ChildComponent? Why does the specific function notation matter? Commented Nov 8, 2021 at 15:31
  • I don't know how it would look like to execute a function call of a props in the child component Commented Nov 8, 2021 at 15:33
  • 1
    A lot of people ask this question about "changing the parent component from a child"... The truth is that you should not do that. The "react" way of doing this is to "share" some kind of state using a "store" like react-redux. Otherwise you will find out soon or later that you shot yourself in the foot. Commented Nov 8, 2021 at 15:33
  • Just add props parameter to your childcomponent and call it as props.func() Commented Nov 8, 2021 at 15:34
  • How do you normally execute a function call? It looks just the same. See e.g. reactjs.org/docs/lifting-state-up.html. Commented Nov 8, 2021 at 15:34

2 Answers 2

2

To pass state changes to the child class you can just pass the functions as attributes to the child. This works for any function and you can even just pass in setMyState if you would like into the child function

 const ParentComponent = () => {
    const [myState, setMyState] = useState(false);
    const changeState = () => setMyState(!myState);

    return(
        <ChildComponent 
            changeState={changeState}
        /> // pass the state variables
    );
}

const ChildComponent = ({changeState}) => { // receive the state variables
    // change state of parent component

    // you can use changeState here and 
    // it will update the parent component and rerender the page
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can pass functions as a prop as well to child components. Try passing the changeState function to the child component like this

const ParentComponent = () => {
    const [myState, setMyState] = useState(false);
    const changeState = () => setMyState(!myState);

    return(
        <ChildComponent handleChange={changeState} /> // pass the state variables
    );
}

const ChildComponent = ({handleChange}) => { // receive the state variables
    // change state of parent component by simply calling 
    // handleChange as a normal function 

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.