Snippets Collections
const Component = ({ receiveAmount, sendAmount }) => {
  const prevAmount = useRef({ receiveAmount, sendAmount }).current;
  useEffect(() => {
    if (prevAmount.receiveAmount !== receiveAmount) {
     // process here
    }
    if (prevAmount.sendAmount !== sendAmount) {
     // process here
    }
    return () => { 
      prevAmount.receiveAmount = receiveAmount;
      prevAmount.sendAmount = sendAmount;
    };
  }, [receiveAmount, sendAmount]);
};
index.js

import MeetupList from "./../components/meetups/MeetupList";
import { useEffect } from "react";
import { useState } from "react";

const data = [
  {
    id: "m1",
    title: "A first meetup",
    image: “/location.jpg”,
    address: "Some address 10, 12345 Some city",
    description: "This is a first meetup!",
  },
  {
    id: "m2",
    title: "A second meetup",
    image:“/location.jpg”,
    address: "Some address 20, 6789 Some city",
    description: "This is a second meetup!",
  },
];

const HomePage = () => {
  const [loadedMeetups, setLoadedMeetups] = useState([]);

  useEffect(() => {
    // send http request and fetch data
    setLoadedMeetups(data);
  }, []);

  return (
    <>
      <MeetupList meetups={loadedMeetups} />
    </>
  );
};

export default HomePage;
star

Thu Apr 28 2022 20:56:22 GMT+0000 (UTC) https://stackoverflow.com/questions/53446020/how-to-compare-oldvalues-and-newvalues-on-react-hooks-useeffect

#javascript #react.js #useeffect
star

Mon Dec 06 2021 14:08:29 GMT+0000 (UTC) https://docs.google.com/document/d/1Fxoh7R_7TExsGfvmNWcryqO9d7-3x0sP3dwXKCZU4Yo/edit

#useeffect

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension