Skip to content
Advertisement

How to update my useEffect hook when State change happen in a different .js file’s component in ReactJS?

I am trying to make an API call in useEffect() and want useEffect() to be called everytime a new data is added in the backend. I made a custom Button(AddUserButton.js) which adds a new user in backend. I am importing this button in the file (ManageUsers.js) where I am trying to display all the users. I just wanted to make an useState to keep track everytime an add button is clicked and make useEffect refresh according to it. For Example:

JavaScript

But currently because I have two .js files, I am not sure how to make my logic stated above work in this case

ManageUsers.js

JavaScript

AddUserButton.js

JavaScript

Advertisement

Answer

A common approach is to pass a callback function to your button component that updates the state of the parent component.

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement