Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for "string" refs will be deprecated #3468

Closed
9 tasks
Gazook89 opened this issue May 10, 2024 · 3 comments · Fixed by #3477
Closed
9 tasks

Support for "string" refs will be deprecated #3468

Gazook89 opened this issue May 10, 2024 · 3 comments · Fixed by #3477
Labels
P2 - minor feature or not urgent Minor bugs or less-popular features solution found A solution exists; just needs to be applied

Comments

@Gazook89
Copy link
Collaborator

Renderer

v3

Browser

Chrome

Operating System

MacOS

What happened?

React is emitting many of these errors in the browser console:

Support for string refs will be removed in a future major release. We recommend using useRef() or createRef() instead.

for the following files:

  • combobox.jsx
  • editor.jsx
  • editPage.jsx
  • homePage.jsx
  • brewRenderer.jsx
  • newPage.jsx
  • printPage.jsx
  • codeEditor.jsx
  • splitPane.jsx

Anything that looks like ref='....' should be changed to use createRef(): More info on old React docs.

This might be a good first issue? Maybe not all at once? Nothing is currently broken and likely won't be for awhile. It is annoying seeing the Errors in console, though.

Code

No response

@Gazook89 Gazook89 added solution found A solution exists; just needs to be applied P2 - minor feature or not urgent Minor bugs or less-popular features labels May 10, 2024
@calculuschild
Copy link
Member

calculuschild commented May 10, 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);
}

@Gazook89
Copy link
Collaborator Author

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?

@calculuschild
Copy link
Member

However you want is fine with me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P2 - minor feature or not urgent Minor bugs or less-popular features solution found A solution exists; just needs to be applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants