2

I am trying to learn React-Spring. One of the codes provided in its documentation throws an error when I run it. Any idea what possibly is wrong? How to solve it?

enter image description here

The code I'm trying to run is-

const TextContent = (props) => {

    const [items] = useState([
        { id: '0', title: 'Text1' },
        { id: '1', title: 'Text2' },
        { id: '2', title: 'Text1' }
    ])

    const [index, setIndex] = useState(0);

    const transitions = useTransition(items[index], index => index.id,
        {
            from: { opacity: 0 },
            enter: { opacity: 1 },
            leave: { opacity: 0 },
            config: { tension: 220, friction: 120 }
        }
    )

    useEffect(() => {
        const interval = setInterval(() => {
            setIndex((state) => (state + 1) % items.length);
        }, 4000)
        return () => clearInterval(interval);
    }, []);

    {
        transitions.map(({ item, props, key }) => (
            <animated.div
                key={key}
                style={{ ...props, position: 'absolute' }}
            >
                <p>
                    {item.title}
                </p>
            </animated.div>
        ))
    }
}

export default TextContent;
Pratyush
  • 163
  • 1
  • 7
  • 15

3 Answers3

0

Add a return statement to your functional component

const TextContent = (props) => {

    const [items] = useState([
        { id: '0', title: 'Text1' },
        { id: '1', title: 'Text2' },
        { id: '2', title: 'Text1' }
    ])

    const [index, setIndex] = useState(0);

    const transitions = useTransition(items[index], index => index.id,
        {
            from: { opacity: 0 },
            enter: { opacity: 1 },
            leave: { opacity: 0 },
            config: { tension: 220, friction: 120 }
        }
    )

    useEffect(() => {
        const interval = setInterval(() => {
            setIndex((state) => (state + 1) % items.length);
        }, 4000)
        return () => clearInterval(interval);
    }, []);

    return (
        <div>
            {
              transitions.map(({ item, props, key }) => (
                <animated.div
                    key={key}
                    style={{ ...props, position: 'absolute' }}
                >
                    <p>{item.title}</p>
                </animated.div>
              ))
            }
        </div>
    )
}

export default TextContent;

Here is a codesandbox where I got it working

Al Duncanson
  • 743
  • 9
  • 16
0

In addition to Al Duncanson answer: My problem was in exporting React Fragment instead of actual tag:

return (
  <>
    { /* springs.map() */ }
  </>
)

Hook started working after I changed it to

return (
  <div>
    { /* springs.map() */ }
  </div>
)
VityaSchel
  • 579
  • 7
  • 18
0

enter image description here

In my NextJs app, I got the same issue. In my case, I think it was a cache-related issue. Run the project after removing the ".next" folder fixed the issue. I hope removing the build folder in React will do the same.

And there are two similar functions ("useSpring" and "useSprings"). Make sure to pick the right one for your use case.

Lojith Vinsuka
  • 906
  • 1
  • 10
  • 8