Advertisement
fakesamgregory

Master React in Webflow - Libraries (Motion)

Nov 17th, 2024 (edited)
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* --------------    
  2.     * -- npm scripts -- *
  3.     npm install motion
  4.  
  5. /* ----------- */
  6. // index.js
  7. import React from "react";
  8. import ReactDOM from "react-dom";
  9. import App from "./App";
  10.  
  11. const root = ReactDOM.createRoot(document.getElementsByClassName("react")[0]);
  12. root.render(<App />);
  13.  
  14. /* ----------- */
  15. // App.js
  16. import React from 'react';
  17. import PokemonList from './PokemonList';
  18.  
  19. export default function App() {
  20.     return (
  21.         <PokemonList />
  22.     );
  23. };
  24.  
  25. /* ----------- */
  26. // PokemonList.js
  27. /*
  28.  * In this instance we are totally faking calling the Pokemon API
  29.  * Just to show a delay/dependency in useGSAP
  30.  * Slightly different use of Motion here but wanted to achieve the same result
  31.  */
  32. import React, { useState, useEffect } from "react";
  33. import { useAnimate } from "motion/react";
  34. import { stagger } from "motion";
  35.  
  36. export default function PokemonList() {
  37.   const [pokemon, setPokemon] = useState([]);
  38.   const [scope, animate] = useAnimate();
  39.  
  40.   useEffect(() => {
  41.     let timeout = null;
  42.  
  43.     const getPokemon = () => {
  44.       // after 1 second, set pokemon
  45.       timeout = setTimeout(() => {
  46.         setPokemon(["bulbasaur", "ivysaur", "venusaur"]);
  47.       }, 1000);
  48.     };
  49.  
  50.     getPokemon();
  51.  
  52.     return () => {
  53.       if (timeout) {
  54.         timeout.clearTimeout();
  55.       }
  56.     };
  57.   }, [setPokemon]);
  58.  
  59.   useEffect(() => {
  60.     // This "li" selector will only select children
  61.     // of the element that receives `scope`.
  62.     if (pokemon.length) {
  63.       animate("li", { opacity: [0, 1] }, { delay: stagger(0.05) });
  64.     }
  65.   }, [pokemon]);
  66.  
  67.   return (
  68.     <ul ref={scope}>
  69.       {pokemon.map((item, index) => (
  70.         <li>{item}</li>
  71.       ))}
  72.     </ul>
  73.   );
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement