I'm using React Transition Group. I have state classFade
, and set classNames
of React Transition Group = classFade
. I want when I click to next button, classFade='fadeRight'
and content will fade to the right. When I click to prev button, classFade='fadeLeft'
and content will fade to the left. But react transition group takes the previous value, not the value after I pass it on. For example, when I clicked Next Button, it received my original classFade
empty value, not 'fadeRight'
, only when click the next button again, will it succeed.
My stage:
const [items, setItems] = useState(
{ id: uuid(), text: 'Buy eggs', classFade: '' }
);
React Transition Group:
<TransitionGroup>
<CSSTransition
key={items.id}
timeout={500}
classNames={items.classFade}
>
{items.text}
</CSSTransition>
</TransitionGroup>
Example when click next button:
setItems({
id: uuid(),
text,
classFade: 'fadeRight',
});
You can check full code and run demo here: https://codesandbox.io/s/transitiongroup-component-8x520