Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Pass Event From Redux To React?

I have a GUI react/redux based app. As part of the view there is a "indicator" react component which I wish to blink, and the blinking is done with a CSS3 animation (animation frames). The indicator.blink() member function of the react component is called to make the indicator blink (it basically removes the blink class from the DOM element, then adds it again 1ms later, as a hack to get around the fact that there is no "restart" api for a CSS3 animation).

Upon certain actions occurring in the redux framework (they can be thunks if needed), I wish to call this blink() function in the react view. How best to do this?

It doesn't feel right to have the redux action modify the app state, and then the indicator element bind to one of the state variables as a prop, since it's not really a state, but an instantaneous event. But I know of no other way to get a redux action to change a react component.

like image 261
gbmhunter Avatar asked Oct 07 '15 23:10

gbmhunter


People also ask

Do React and Redux go together?

Integrating Redux with a UI​ You can write Redux apps with React, Vue, Angular, Ember, jQuery, or vanilla JavaScript. That said, Redux was specifically designed to work well with React. React lets you describe your UI as a function of your state, and Redux contains state and updates it in response to actions.

Can I use Redux connect with React hooks?

We recommend using the React-Redux hooks API as the default approach in your React components. The existing connect API still works and will continue to be supported, but the hooks API is simpler and works better with TypeScript.


1 Answers

Suppose you want to use the blink thing to show when something increases, you can simply keep a counter in your state and in your component's internal state keep the previous value. When it changes, blink and save the new value.

In other words, derive your desired event information from state changes you care about.

It is totally fine to use internal state for this sort of transient behavior, that is what it is for.

like image 166
w00t Avatar answered Sep 30 '22 12:09

w00t