Giter VIP home page Giter VIP logo

Comments (3)

calculuschild avatar calculuschild commented on September 25, 2024

We should gradually convert all of these to functional components anyway (which uses "useRef" instead of "createRef"). Here are my personal notes on how to use REFS with functional components:


Refs

Why use Refs?

By using a ref, you ensure that:

  • You can store information between re-renders (unlike regular variables, which reset on every render).
  • Changing it does not trigger a re-render (unlike state variables, which trigger a re-render).
  • The information is local to each instance of your component (unlike global variables, which are shared).

Changing a ref does not trigger a re-render, so refs are not appropriate for storing information you want to display on the screen. Use state for that instead.

How to use

Call useRef at the top level of your component to declare one or more refs.

import { useRef } from 'react';

function Stopwatch() {
	const intervalRef = useRef(0);
	// ...

useRef returns a ref object with a single current property initially set to the initial value you provided.

On the next renders, useRef will return the same object. You can change its current property to store information and read it later. This might remind you of state, but there is an important difference.

Changing a ref does not trigger a re-render. This means refs are perfect for storing information that doesn’t affect the visual output of your component. For example, if you need to store an interval ID and retrieve it later, you can put it in a ref. To update the value inside the ref, you need to manually change its current property:

function handleStartClick() {
	const intervalId = setInterval(() => {
		// ...
	}, 1000);
	intervalRef.current = intervalId;
}

Later, you can read that interval ID from the ref so that you can call clear that interval:

function handleStopClick() {
	const intervalId = intervalRef.current;
	clearInterval(intervalId);
}

from homebrewery.

Gazook89 avatar Gazook89 commented on September 25, 2024

Do you want to combine this ref work with functional component work? If so, this issue could be renamed to indicate the much larger task of doing that (and refs would be one small part). Perhaps as a Project, and separate out each component as a separate Issue?

from homebrewery.

calculuschild avatar calculuschild commented on September 25, 2024

However you want is fine with me.

from homebrewery.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.