Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redux/Flux (with ReactJS) and animation

I'm learning React+Redux and I don't understand the proper way of doing the animations. Lets speak by example:

For instance, I have a list and I would like to remove items on click. That's super easy if I have no animation effects there: dispatch REMOVE_ITEM action on click, reducer removes the item from the store and react re-renders html.

Let's add an animation of deleting the line item on click. So, when user clicks on an item I want to run a fancy effect of line item removal and... how? I can think of several ways how to do it:

1) On click I dispatch REMOVE_ITEM action, then reducer mark an item as goingToBeDeleted in Store, then react renders that element with a class of .fancy-dissolve-animation and I run a timer to dispatch the second action REMOVE_ITEM_COMPLETED. I don't like this idea, because it's still unclear how to add JS animations here (for example, with TweenMax) and I run a JS timer to re-render when CSS animation ends. Doesn't sound good.

2) I dispatch ITEM_REMOVE_PROGRESS actions with an interval of ~30ms, and store holds some "value" which represents the current state of animation. I don't like it too, as it would require me to copy the store ~120 times for ~2 seconds of animation (say, I want smooth 60 fps animation) and that's simply a waste of memory.

3) Make an animation and dispatch REMOVE_ITEM only after animation finishes. That's the most appropriate way I can think of, but still I'd like to have things changed in store right after user makes the action. For example, animation may take longer than few seconds and REMOVE_ITEM might sync with a backend – there's no reason to wait animation finish to make a backend API call.

Thanks for reading – any suggestions?

like image 254
shlajin Avatar asked Oct 13 '16 10:10

shlajin


People also ask

Can you explain why you would use React with Redux or flux?

But Redux works really well with React. Redux basically provides a way for managing the store, unidirectional flow of data and much more. Flux architecture has multiple stores that are coordinated together by the dispatcher and when there is a change all the stores need to update themselves.

What is flux and Redux in react JS?

The primary difference of Flux vs Redux is that Flux includes multiple Stores per app, but Redux includes a single Store per app. Rather than placing state information in multiple Stores across the application, Redux keeps everything in one region of the app.

Which is better Redux or flux?

FLUX is architecture, and REDUX is a library. FLUX is more suitable as an application architecture for a building application user interface. Facebook uses flux application architecture for creating client-side web-based applications. It complements React's composable view with a unidirectional data flow.

Does Redux use flux?

Redux is a library inspired by Flux and can be considered as an implementation of Flux. Redux makes easy to handle the state of the application and manage to display data on user actions. It is a very powerful library but also very lightweight. Redux is a predictable state container for JavaScript apps.


1 Answers

React has a great solution to this problem in the ReactCSSTransitionGroup helper class (see https://facebook.github.io/react/docs/animation.html). With this option, React takes care of it for you, by keeping the DOM state for the child as it was at the last render. You simply wrap your items in a ReactCSSTransitionGroup object. This keeps track of its children, and when it is rendered with a child removed, instead of rendering without the child, it renders with the child, but adds a CSS class to the child (which you can use to trigger a CSS animation, or you can just use CSS transitions for simplicity). Then, after a timeout (configured as a prop passed to ReactCSSTransitionGroup), it will re-render again, with the child removed from the DOM.

To use ReactCSSTransitionGroup, you'll need to npm install react-addons-css-transition-group, and then require/import 'react-addons-css-transition-group'. The animation docs give more detailed information.

One thing to remember - make sure the children have unique, unchanging keys. Just using the index as the key will make it behave incorrectly.

like image 50
TomW Avatar answered Oct 10 '22 04:10

TomW