- Forums
- react
- React useRef() - My Notes How To Use useRef With Examples
This Page Contains information about React useRef() - My Notes How To Use useRef With Examples By dd in category react with 0 Replies. [5110], Last Updated: Mon Jun 24, 2024
dd
Sat Mar 25, 2023
0 Comments
286 Visits
React UseRef() can be use to store element values, store values outisde of the render or store previous variable values, example, counter...
Resource: https://www.webune.com/forums/react-ionic-auto-click-button-trigger-click-programmatically-automatically-without-human-click.html
1. Import:
import React, {useRef } from 'react';
2. Declare variable
const errorDiv = useRef();
3. Use in JSX:
<div ref={errorDiv}>this is the error i got</div>
4. Event
<Button variant="primary" onClick={()=>ErrorMessage()}>Show Modal</Button>
5. Execute
const ErrorMessage = (e) =>{
errorDiv.current.click(); // trigger click on errorDiv
console.log(`LINE 14 errorDiv=`, errorDiv);
}
6. Results
View Console Log
7. Resource Video:
t2ypzz6gJm0