Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

previous state in useEffect


function usePrevious(value) {
  const ref = useRef();
  useEffect(() => {
    ref.current = value;
  });
  return ref.current;
}

const Component = (props) => {
    const {receiveAmount, sendAmount } = props
    const prevAmount = usePrevious({receiveAmount, sendAmount});
    useEffect(() => {
        if(prevAmount.receiveAmount !== receiveAmount) {

         // process here
        }
        if(prevAmount.sendAmount !== sendAmount) {

         // process here
        }
    }, [receiveAmount, sendAmount])
}

Comment

usestate previous state

const Foo = (props) => {
  const [name, updateName] = useState('Doe');

  return (
    <div>
      <div>{name}</div>
      <button
        onClick={() => updateName((prevState) => (
          `Old value was ${prevState}`
    	  )
        )}
      >
        Click me
      </button>
    </div>
    )
}


export default Foo;
Comment

when to use previous state in useState

import React, { useState } from "react";
import ReactDOM from "react-dom";

function Counter() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <h1>{count}</h1>
      <button onClick={() => setTimeout(() => setCount(count + 1), 2000)}>
        Delayed Counter (basic)
      </button>
      <button onClick={() => setTimeout(() => setCount(x => x + 1), 2000)}>
        Delayed Counter (functional)
      </button>
      <button onClick={() => setCount(count + 1)}>Immediate Counter</button>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<Counter />, rootElement);
Comment

usestate access previous state

const [arrayOfObjs, handleObjSelection] = useState([]);

// on a buttton for example
<button
  onClick={selectedObj => handleObjSelection(
              prevSelected => [...prevSelected, selectedObj],
  		  ))}
>
Comment

PREVIOUS NEXT
Code Example
Javascript :: js console log function code 
Javascript :: javascript get all instances of a class 
Javascript :: toast show pb 
Javascript :: onchange vue 
Javascript :: path object d3.js 
Javascript :: javascript reflect 
Javascript :: Format javascript date with date.js library 
Javascript :: angular content-security-policy header 
Javascript :: js electron setup 
Javascript :: sequelize attributes exclude all 
Javascript :: react navigation header title 
Javascript :: js get variable from url 
Javascript :: javascript if equal infinity 
Javascript :: how to stop angular server 
Javascript :: an array of functions 
Javascript :: do while loop javascript 
Javascript :: read and save excel with react 
Javascript :: Getting One Value from an Array of Items 
Javascript :: electron js 
Javascript :: Change Title In React Project 
Javascript :: example custom theme material ui 
Javascript :: set placeholder javascript 
Javascript :: connect redux 
Javascript :: html table to csv javascript 
Javascript :: array.filter 
Javascript :: Ternary Expressions in JavaScript 
Javascript :: how to return when child process is complete in node js 
Javascript :: var = {} js 
Javascript :: jquery OR operation 
Javascript :: how to get checked and unchecked checkbox value in jquery 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =