Creating refs in React
ReactJS Refs can be created using React.createRef() function and attached to a React element via the ref attribute.
When a class component is constructed, the Refs are commonly assigned to an instance property so that they can be referenced in the component.
Example
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.myCallRef = React.createRef();
}
render() {
return <div ref={this.myCallRef} />;
}
}
Detailed guide on How to create refs in React JS?
ReactJS Refs
ReactJS Refs are used to access and modify the DOM elements in the React Application. It creates a reference to the elements and uses it to modify them.
Table of Content
- What are React refs ?
- Creating refs in React
- Accessing Refs in React
- When to use refs
- When not to use refs