mercredi 7 octobre 2020

ReactJS : how can I pass a child component to its ancestor?

i'm working on a music app, but I'm quite new to ReactJS.

The structure of the components looks like this :

- Playlist
-- Player
-- Tracklist
--- Track
--- Track
--- Track

I would like to have a currentTrack state in my Playlist component, and the value of it would be a reference to the Track component that has been clicked (not just some data from that track).

Because I need to be able to access the Track's method from within the Playlist component : for instance, when a Track will be defined as the current one in the Playlist, there might be an old track that I need to unload. I would thus need to update the state or call some methods in that old track before setting the new one.

My components use React hooks, they are not classes.

How could I pass a reference of my Track component up to the Playlist component after it is clicked ?

Thanks !

Aucun commentaire:

Enregistrer un commentaire