I'm trying to animate the position change of items in a list with React Spring.
Having an item fade in with useSpring
is straightforward:
function App() {
const [items, setItems] = React.useState([0, 1]);
const handleClick = () => {
if (items[0] === 0) {
setItems([1, 0]);
} else {
setItems([0, 1]);
}
};
const props = useSpring({ opacity: 1, from: { opacity: 0 } });
return (
<>
<animated.div style={props}>
<button type="button" onClick={handleClick}>
click
</button>
</animated.div>
<ul className="App">
{items.map(item => {
return <li key={item}>{item}</li>;
})}
</ul>
</>
);
}
However when I try and use useSprings
I get an error:
function App() {
const [items, setItems] = React.useState([0, 1]);
const handleClick = () => {
if (items[0] === 0) {
setItems([1, 0]);
} else {
setItems([0, 1]);
}
};
const springs = useSprings(items.length, items.map(item => ({ opacity: item.opacity }))
return (
<>
<button type="button" onClick={handleClick}>
click
</button>
<ul className="App">
{items.map(item => {
return <li key={item}>{item}</li>;
})}
</ul>
</>
);
}
https://codesandbox.io/s/wonderful-oskar-v04vw
/src/index.js: Unexpected token, expected ","
The docs don't go into much detail: https://www.react-spring.io/docs/hooks/use-springs