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
Table of contents Examples from real life Slow qu...
This article example shares the specific code of ...
Table of contents Business requirements: Solution...
Table of contents Demand background Thought Analy...
This article example shares the specific code of ...
Situation description: The database was started a...
Table of contents Abstraction and reuse Serial Se...
Table of contents Objectives for this period 1. F...
Table of contents Understanding Prototypes Unders...
Table of contents 1. Solution 2. Let the browser ...
Table of contents From father to son From son to ...
Say it in advance On a whim, I want to know what ...
This article example shares the specific code for...
This article shares with you how to use the Vue c...
Preface Whether it is Oracle or MySQL, the new fe...