Logo

dev-resources.site

for different kinds of informations.

Using React useRef Hook to access immediate past props or state.

Published at
2/10/2022
Categories
javascript
react
hooks
useref
Author
walecloud
Categories
4 categories in total
javascript
open
react
open
hooks
open
useref
open
Author
9 person written this
walecloud
open
Using React useRef Hook to access immediate past props or state.

originally posted on my personal blog walecloud.me

Using React useRef Hook to access immediate past props or state

Do you want to know what the previous state of a value is before it got updated in React?
You can easily leverage useRef to track the previous value of state or props in React.

Recently, while working on a project built with React and Firebase, we had a use-case for knowing what the previous state of a value was. This need came to be when a state item needed to be reset if an ID from firebase changes.

The useRef hook in react is ideal for things like this, you probably thought its sole purpose is for DOM manipulation but it can be more and almost anything you want it to be.

TL;DR

// usePrevious hook React official documentation

import { useEffect, useRef } from 'react';

export const usePrevious = (value) => {
  const ref = useRef();
  useEffect(() => {
    ref.current = value;
  }, [value]);
  return ref.current;
};
Enter fullscreen mode Exit fullscreen mode

// use returned value like so;

  const prevModuleId = usePrevious(moduleId);
Enter fullscreen mode Exit fullscreen mode

Hopefully Reacts make the usePrevious into an official hook soon as it seems like a no-brainer.

How does the usePrevious hook works?

Short answer:

  • useRef: A container that is useful to keep a mutable (changeable) value in its .current property
  • useEffect: Allows for monitoring changes and performing side effects in functional components.

You can read more about both hooks on the React official site

First, we create an instance of Ref whenever the hook is called.
The useEffect only runs when the value parameter changes and then assign that to the ref's .current property
Finally, we return the ref.current.

The first time the hook is called, ref.current will be undefined until a state or props value changes until then before the useEffect hook is executed to reflect the latest previous value of the parameter.


Find this helpful? Kindly share so others can too.
cheers 🥂

useref Article's
29 articles in total
Favicon
How to create components interfaces using useRef/forwardRef/useImperativeHandle
Favicon
Hooks Behind The Scenes 3, UseRef!!!
Favicon
Mastering React's useRef Hook: Working with DOM and Mutable Values
Favicon
Understanding useRef: A Beginners Guide
Favicon
useRef Hook Explained
Favicon
useRef() in React . . .
Favicon
React State Management: When & Where add your states?
Favicon
Unlocking the Power of useRef: Besic to Advanced Examples for React Developers
Favicon
Leveraging useRef in TypeScript for Efficient DOM Manipulation
Favicon
Unlocking the Power of React Hooks
Favicon
React.useRefの理解を含めるエクササイズ
Favicon
Mastering React's useRef Hook: A Deep Dive
Favicon
Unlocking the Power of useRef in React: 12 Essential Use Cases
Favicon
Série React Hooks: useRef
Favicon
Toggle Password Visibility Using React useRef
Favicon
Stop using useState for everything
Favicon
Using React hooks to develop a Video Player
Favicon
React - How to load a form with the latest data
Favicon
React: сфокусировать поле ввода по чекбоксу
Favicon
How do I check/uncheck all checkboxes with a button In React Js using useRef() Hook ?
Favicon
Multiple item using one ref
Favicon
Closures and useEffects
Favicon
I need help. TypeError: Cannot read properties of undefined (reading 'current')
Favicon
Using React useRef Hook to access immediate past props or state.
Favicon
Compare Props in React Functional Components.
Favicon
Creating infinitely scrolling SPA using React
Favicon
useRef()가 순수 자바스크립트 객체라는 의미를 곱씹어보기
Favicon
Complete useRef() hook with live code examples
Favicon
Useful Patterns with React hooks

Featured ones: