r/reactjs•
petermlm

Help understanding where to fetch and how to pass data around.

Needs Help

I have the following components:

  • A buttons component that has the buttons: Last month, last day, last hour
  • A set of data display components which show some data from an endpoint each
  • A main component that has the buttons and the data display components has children.

Currently, each data display component will fetch their data in their respective componentDidMount function. This is done by calling an endpoint without query parameters.

What I want to do is: I want to click in a button, and make all of the data display components fetch data from the endpoint again, but this time with a query parameter like: range=month.

I've added a callback function via prop to the buttons component. Like this the main component can "know" that one of the buttons have been clicked, and I can update the state of the main component.

But I'm not sure how to implement all of this in a conventional way. Like, do I pass the new range value to the child components thereby making them rerender? If so, where should I fetch the data for those component? In the render function?

Thanks in advance!

Posted Dec 21, 2020, 7:44 PM UTC

12
Loading comments…