A Thoughtful Way To Use React’s useRef() Hook

By Smashing Magazine - 2020-11-19

Description

In a React component, useState and useReducer can cause your component to re-render each time there is a call to the update functions. In this article, you will find out how to use the useRef() hook t ...

Summary

  • In a React component, useState and useReducer can cause your component to re-render each time there is a call to the update functions.
  • Initially, the component initializes all variables to the default values, also stores all the state and refs to a unique store as defined by the React algorithm.
  • When a new update is available for the component through an update to its props or state, React pulls the old value for states and refs from its store and re-initializes the state to the old value also applying an update to the states and refs that have an update.
  • Storing values in refs solve the problem of frequent re-rendering but brought a new challenge of the component not being updated after a ref’s value has changed which can be solved by introducing a setForceUpdate state update function.

 

Topics

  1. Frontend (0.42)
  2. Mobile (0.15)
  3. Machine_Learning (0.09)

Similar Articles

sudheerj/reactjs-interview-questions

By GitHub - 2021-01-12

List of top 500 ReactJS Interview Questions & Answers....Coding exercise questions are coming soon!! - sudheerj/reactjs-interview-questions

React Hooks Cheatsheet: The 7 Hooks You Need To Know

By freeCodeCamp.org - 2021-02-08

This new tutorial will show you everything you need to know about React Hooks from scratch. I've put this cheatsheet together to help you become knowledgeable and effective with React Hooks as quickly ...

Write Vue like you write React

By DEV Community - 2021-02-02

With the Vue 3 Composition API, you can write functional components. What happens if we write them with JSX templates? Are they similar to React functional components?. Tagged with react, vue, javascr ...