React refs are used to access the DOM or React elements that were created in the render method. According to the
Three ways used to create refs in React are:
createRef()
refs (this is a recent addition)In the examples below, a ref is used to access the user input and set the background color to a specified color.
The following code snippet demonstrates how a ref can be created using a callback:
import React from 'react'; require('./style.css'); import ReactDOM from 'react-dom'; import App from './app.js'; ReactDOM.render( <App />, document.getElementById('root') );
createRef()
Starting from version 16.3, React included a new createRef()
method that can be used for creating refs. The following code snippet demonstrates how a ref can be created using the createRef()
method:
import React from 'react'; require('./style.css'); import ReactDOM from 'react-dom'; import App from './app.js'; ReactDOM.render( <App />, document.getElementById('root') );
Free Resources