createRefLink for this heading
January 6, 2025 · View on GitHub
Pitfall
createRef is mostly used for class components. Function components typically rely on useRef instead.
createRef creates a ref object which can contain arbitrary value.
class MyInput extends Component {
inputRef = createRef();
// ...
}
ReferenceLink for Reference
createRef()Link for this heading
Call createRef to declare a ref inside a class component.
import { createRef, Component } from 'react';
class MyComponent extends Component {
intervalRef = createRef();
inputRef = createRef();
// ...
ParametersLink for Parameters
createRef takes no parameters.
ReturnsLink for Returns
createRef returns an object with a single property:
current: Initially, it’s set to thenull. You can later set it to something else. If you pass the ref object to React as arefattribute to a JSX node, React will set itscurrentproperty.
CaveatsLink for Caveats
createRefalways returns a different object. It’s equivalent to writing{ current: null }yourself.- In a function component, you probably want
useRefinstead which always returns the same object. const ref = useRef()is equivalent toconst [ref, _] = useState(() => createRef(null)).
UsageLink for Usage
Declaring a ref in a class componentLink for Declaring a ref in a class component
To declare a ref inside a class component, call createRef and assign its result to a class field:
import { Component, createRef } from 'react';
class Form extends Component {
inputRef = createRef();
// ...
}
If you now pass ref={this.inputRef} to an <input> in your JSX, React will populate this.inputRef.current with the input DOM node. For example, here is how you make a button that focuses the input:
App.js
App.js
ResetFork
import { Component, createRef } from 'react';
export default class Form extends Component {
inputRef = createRef();
handleClick = () => {
this.inputRef.current.focus();
}
render() {
return (
<>
<input ref={this.inputRef} />
<button onClick={this.handleClick}>
Focus the input
</button>
</>
);
}
}
Show more
Pitfall
createRef is mostly used for class components. Function components typically rely on useRef instead.
AlternativesLink for Alternatives
Migrating from a class with createRef to a function with useRefLink for this heading
We recommend using function components instead of class components in new code. If you have some existing class components using createRef, here is how you can convert them. This is the original code:
App.js
App.js
ResetFork
import { Component, createRef } from 'react';
export default class Form extends Component {
inputRef = createRef();
handleClick = () => {
this.inputRef.current.focus();
}
render() {
return (
<>
<input ref={this.inputRef} />
<button onClick={this.handleClick}>
Focus the input
</button>
</>
);
}
}
Show more
When you convert this component from a class to a function, replace calls to createRef with calls to useRef:
App.js
App.js
ResetFork
import { useRef } from 'react';
export default function Form() {
const inputRef = useRef(null);
function handleClick() {
inputRef.current.focus();
}
return (
<>
<input ref={inputRef} />
<button onClick={handleClick}>
Focus the input
</button>
</>
);
}
Show more
Copyright © Meta Platforms, Inc
no uwu plz
uwu?
Logo by@sawaratsuki1004
More
On this page
- Overview
- Reference
createRef()- Usage
- Declaring a ref in a class component
- Alternatives
- Migrating from a class with
createRefto a function withuseRef
Search⌘CtrlK
-
react@19
- Overview
- Hooks
- Components
- APIs
-
react-dom@19
- Hooks
- Components
- APIs
- Client APIs
- Server APIs
- Static APIs
-
Rules of React
- Overview
-
React Server Components
- Server Components
- Server Functions
- Directives
-
Legacy APIs
- Legacy React APIs
Is this page useful?