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
A simple license plate input component (vue) for ...
Preview knowledge points. Animation Frames Backgr...
1. Always close HTML tags In the source code of p...
vmware workstations starts the virtual machine er...
In a complex table structure, some cells span mul...
Table of contents 1. Introduction 2. Solution Imp...
Panther started as a rookie, and I am still a roo...
In this section, we will learn about list element...
1. Introduction pt-query-digest is a tool for ana...
Recently, our small team needs to share a shared ...
Preface With the crazy speculation of virtual cur...
Table of contents Overview 1. useState 1.1 Three ...
lsof (list open files) is a tool to view files op...
Anyone who has worked on a large system knows tha...
【1】Know the width and height of the centered elem...