vendredi 4 mai 2018

How should a React component call a function in a collection of homogenous children?

Say I have a component that contains functionality to send PUT requests to my backend. Normally, these requests are triggered by the user interacting with this component directly. Let's call this Child.

A different part of my UI contains a collection of these elements rendered in a parent component. Let's call this new container Parent.

I want Parent to have a common button rendered next to the collection of Child components that lets my user trigger PUT requests from all Child elements in one go.

Currently, I save refs of each Child element as I render each of them in Parent. When the user clicks the common button rendered in Parent, I have Parent iterate over each ref and invoke the function in each Child that triggers the PUT request. Is there a better way?

Aucun commentaire:

Enregistrer un commentaire