Search code examples
reactjsspringreact-spring

How to make react-spring <Spring> replay animation?


I have a simple react site which displays drinks from the drinks-api. It basically works like this: when user presses button, the query gets updated, fetchItems runs and items are displayed this way

<Spring from={{ opacity: 0, marginTop: 100 }} to={{ opacity: 1, marginTop: 0 }} config={{ delay: 400 }}>
            {(props) => (
                <div style={props}>
                    {drinks
                        ? drinks.map((drink) => (
                              <DrinkCard
                                  title={drink.strDrink}
                                  image={drink.strDrinkThumb}
                                  alcohol={drink.strAlcoholic}
                                  category={drink.strCategory}
                              />
                          ))
                        : () => (
                              <div className="col-6 col-md-4 p-0">
                                  <h1>NO RESULTS</h1>
                              </div>
                          )}
                </div>
            )}
        </Spring>

For now this animation works only once when page loads. How do I force it to run on every search?


Solution

  • You should add reset to your Spring component like this:

      <Spring
        from={{ opacity: 0, marginTop: 100 }}
        to={{ opacity: 1, marginTop: 0 }}
        config={{ delay: 400 }}
        reset
      >
    

    Here is an example: https://codesandbox.io/s/dank-moon-sq7v9?file=/src/App.js:348-503