Element values shifting and changing before transition with react-transition-group
Adding specific identifier in your array and setting key of each element to each identifier in map method will solve your problem.
import React from "react";
import ReactDOM from "react-dom";
import { Button } from "react-bootstrap";
import { CSSTransition, TransitionGroup } from "react-transition-group";
import "bootstrap/dist/css/bootstrap.css";
import "./styles.css";
import uuid from "uuid";
function App() {
const [items, setitems] = React.useState([
{ id: uuid(), name: "Dairy Free" },
{ id: uuid(), name: "Party Food" },
{ id: uuid(), name: "Family Sized" },
{ id: uuid(), name: "Low Cholesterol" },
{ id: uuid(), name: "Low Sodium" }
]);
const removeitem = item => {
setitems(items.filter(itemname => itemname.id !== item));
};
return (
<div className="App">
<TransitionGroup className="todo-list">
{items.map(data => (
<CSSTransition timeout={500} classNames="item" key={data.id}>
<Button className="m-2" onClick={() => removeitem(data.id)}>
{data.name}
</Button>
</CSSTransition>
))}
</TransitionGroup>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
This problem is already solved here, so please checkout to understand this strange behavior.