Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to re-render a parent from a children component with hook?

Tags:

reactjs

I would like to force my parent to re-render the page when I click on a button from a child component. (I don't use redux because I don't have the time to learn it in my project, so I use localStorage. Unfortunately React don't see when a change is operated on local Storage, so he don't re-render. It's why I would like to force it to re-render my page (to have the right content).)

I tried to use hook with the function useState to do it but it's not working and I don't know why... (Nothing change in my page)

This is my parent page: (just the code important)

const[reload, setReload] = useState(false);
...
else if (user) { contents = [<Message_UserIdentified user={user} callBack={setReload}/>, contentform]; }

This is my child component:

const Message_UserIdentified = (props) => {
    let user = props.user;
    return (
        <Alert color="primary" className="alert alert-dismissible alert-info">
            <h4>Welcome {!user ? "" : user.firstname} {!user ? "" : user.lastname}</h4>
            If you are not {!user  ? "" : user.firstname} click <a onClick={() => {localStorage.removeItem('idUser'); props.callBack(true);}}>here.</a>
        </Alert>
    );
}

Why my parent page don't want re-render ? Thanks in advance.

like image 912
Malaury Boudon Avatar asked Oct 09 '19 00:10

Malaury Boudon


People also ask

How do I transfer data from child components to parents?

To pass data from child to parent component in React:Pass a function as a prop to the Child component. Call the function in the Child component and pass the data as arguments. Access the data in the function in the Parent .

How do you pass data from a child component to its parent in Reactjs hooks?

First, you'll need to create two components, one parent and one child. Next, you'll import the child component in the parent component and return it. Then you'll create a function and a button to trigger that function. Also, you'll create a state using the useState Hook to manage the data.

How do you update the parent component from a child component in React hook?

React hooks are introduced in React 16.8. If you are familiar with the class components then there is no difference to change the parent component state from child component. In both cases, you have to pass the callback function to the parent.

How do you call a method in parent component from a child component?

To call a parent's function from a child component, pass the function reference to the child component as a prop. Then you can call that parent's function from the child component like props. parentMethodName().


1 Answers

I have created a proof of concept of what you are trying to achieve and it works:

https://codesandbox.io/s/weathered-smoke-ojr5j

probably there's something else in your code that we can't see that's preventing the component to re render

like image 134
brein Avatar answered Sep 22 '22 04:09

brein