G
G
Grigory Shein2020-07-24 09:04:16
React
Grigory Shein, 2020-07-24 09:04:16

How to pass events between react.js components?

I have a task so that when a button in one component is clicked, an event in another one fires.

I know that you can do this through props and refs, but it seems strange to me to transfer functions from component to component (maybe I'm not used to it).
You can probably also hang a listener on the window, but this is also somehow ugly.
Is there any way to keep track of the state if I'm using redux?

On vue, I would do this:
https://www.digitalocean.com/community/tutorials/v...

It's just strange that in such a popular framework so many dances are needed for the simplest function

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vlad Karpov, 2020-07-24
@djokerik

You use a function by calling from a reducer, import the called function into the reducer of the desired component, wrap it in a sledge and call it as your function in the desired component

S
Sergey Suntsev, 2020-07-24
@GreyCrew

In fact, this is not dancing, but a stable architecture that allows you to scale the application to huge sizes, without shooting off the legs)
1) You must have an action for each event.
2) Actions you must add to the reducer to track the call (observer pattern)
3) Dispatch the calls to all the components you need.
article on them
off dock
PS. If you need to do it urgently and you have a small application, then you can use something else instead of redux, for example mobx , or native Context

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question