1. useState hook //Import useState from react import React, { useState } from 'react'; function Count() { //Declare a new state variable called count const [count, setCount] = useState(0); 2. useRef hook We can also store the user input in refs and display the collected data as follows: //Import useRef hook import React, { useRef } from "react" export default function App() { //Create a variable to store the reference const nameRef = useRef(); function handleSubmit(e) { //Prevent the page from reloading on submit e.preventDefault() // Output name console.log(nameRef.current.value) } return ( <div className="container"> <form onSubmit={handleSubmit}> <div className="input_group"> <label>Name</label> <input type="text" ref={nameRef}/> </div> <input type="submit"/> </form> </div> ) } 3. useRef and useState
4. When to use Refs and States So in summary, these two This is the end of this article about You may also be interested in:
|
<<: The latest collection of 18 green style web design works
>>: Detailed explanation of redo log and undo log in MySQL
Browser compatibility is the most important part ...
This article records the detailed process of down...
Table of contents 1. Download JDK (take jdk1.8.0 ...
As shown below: Yesterday: UNIX_TIMESTAMP(CAST(SY...
[LeetCode] 177.Nth Highest Salary Write a SQL que...
This article example shares the specific code of ...
After the image is built successfully, it can be ...
letter-spacing property : Increase or decrease th...
Nginx log description Through access logs, you ca...
Download image docker pull openjdk Creating a Dat...
This article lists some tips and codes about form...
Business requirements One of the projects I have ...
Method 1: Use the SET PASSWORD command mysql> ...
A web server can build multiple web sites with in...
CSS: Copy code The code is as follows: *{margin:0;...