Keeping track of the last value of state in React.js

1 · Amit Merchant · June 21, 2022, 5:51 p.m.
When working with states in React.js, there comes a time when you might need to differentiate between the current and previous “state” of a state. For instance, you’ve written an event that should only get triggered if the difference between the current and previous state matches a certain value. Check this simple example. import { useState } from "react"; export default function App() { const [name, setName] = useState("Amit"); return ( <> <h1>Current name: {name}</h1> <bu...