2

I'm just new to React Hooks, and playaround with it.

Just wondering if there any solutions that I could use useEffect to fetch data several times after componentDidMount.

Normally we use useEffect to fetch data based on some props change by passing them.

useEffect(()=> {
fetchApisomefunction()=>{}
},
[]); // normally do this for componentDidMount

How can I fetch data again after that?


UPDATE

callback function could solve this.

1 Answer 1

1

Assuming that you are not using Redux or Context API,

so component A and B are not communicating to each other directly.

On the other hand, there will be a parent component which joins those two components.

As you've said, component B will post the data, and that should change parent's state.

Since react child component is being re-rendered whenever its parent component's state or props change, component A will also be re-rendered.

So all you have to do is to use useEffect without second parameter [].

Actually it is not the question of using Hooks, but the question of react component structure.

Keep in mind that the two child components should not directly communicate to each other.

Sign up to request clarification or add additional context in comments.

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.