N
N
Nessunskill2022-03-20 22:02:20
React
Nessunskill, 2022-03-20 22:02:20

How to call a function on all react components by clicking on one button?

There is an application:

2 buttons - add a car, start the game
when you click on add a car, I add the Car component to the page, they have their own properties (id, color, move function)

how do I now, when I click on the Start game button, cause movement for all cars ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
n1ksON, 2022-03-20
@n1ksON

You create a state in the main component using a hook (or using Redux), create a state update function in the same place. When the button is clicked, an event should be fired that will call the state update function. And forward the updated data using props
useState
props

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question