Updated 27/03/2022
Returns a stateful value, and a function to update it.
JSX
Code example by Brian Holt.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| import { useState } from "react";
const ANIMALS = ["bird", "cat", "dog", "rabbit", "reptile"];
const SearchParams = () => { const [animal, updateAnimal] = useState(""); const [location, updateLocation] = useState(""); const [breed, updateBreed] = useState(""); const breeds = [];
return ( <div className="search-params"> <form> <label htmlFor="location"> Location <input id="location" value={location} placeholder="Location" onChange={(e) => updateLocation(e.target.value)} /> </label> <label htmlFor="animal"> Animal <select id="animal" value={animal} onChange={(e) => updateAnimal(e.target.value)} onBlur={(e) => updateAnimal(e.target.value)} > <option /> {ANIMALS.map((animal) => ( <option key={animal} value={animal}> {animal} </option> ))} </select> </label> <label htmlFor="breed"> Breed <select disabled={!breeds.length} id="breed" value={breed} onChange={(e) => updateBreed(e.target.value)} onBlur={(e) => updateBreed(e.target.value)} > <option /> {breeds.map((breed) => ( <option key={breed} value={breed}> {breed} </option> ))} </select> </label> <button>Submit</button> </form> </div> ); };
export default SearchParams;
|
React.FC
Code example by Ibrahima Ndaw.
1 2 3 4 5 6 7 8 9 10 11 12 13
| import * as React from "react";
export const App: React.FC = () => { const [counter, setCounter] = React.useState<number>(0) return ( <div className="App"> <h1>Result: { counter }</h1> <button onClick={() => setCounter(counter + 1)}>+</button> <button onClick={() => setCounter(counter - 1)}>-</button> </div> ); }
|
References