Home >Web Front-end >JS Tutorial >How to Select DOM Elements in React: Alternatives to `document.getElementById()`?
In React, unlike working with vanilla JavaScript, accessing DOM elements directly is different. React uses a virtual DOM to efficiently update the real DOM, this makes it different from vanilla JavaScript.
Option 1: Using Refs
Option 2: Using React.createRef (v16.3 )
Option 3: Using Callback Pattern (Legacy)
Option 4: Using String Refs (Legacy)
Below is an example of using the callback pattern to select a DOM element:
render() { return ( <div> <Progressbar completed={25} >
To access the elements, you can use this.progressBars[0], this.progressBars[1], and this.progressBars[2] to perform operations on them.
The above is the detailed content of How to Select DOM Elements in React: Alternatives to `document.getElementById()`?. For more information, please follow other related articles on the PHP Chinese website!