|
| 1 | +import React from "react"; |
| 2 | +import ActionButton from "../../commons/ActionButton"; |
| 3 | +import { useLog } from "../../commons/ExampleBloc"; |
| 4 | + |
| 5 | +const ExampleUseState102 = () => { |
| 6 | + const log = useLog(); |
| 7 | + |
| 8 | + const [numberState, setNumberState] = React.useState<number>(0); |
| 9 | + const [stringState, setStringState] = React.useState<string>(""); |
| 10 | + const [objectState, setObjectState] = React.useState<{ insideVal: string }>({ |
| 11 | + insideVal: "" |
| 12 | + }); |
| 13 | + log("virtual-render", { numberState, stringState, objectState }); |
| 14 | + |
| 15 | + return ( |
| 16 | + <> |
| 17 | + <pre> |
| 18 | + {JSON.stringify({ numberState, stringState, objectState }, null, 2)} |
| 19 | + </pre> |
| 20 | + |
| 21 | + <ul> |
| 22 | + <li> |
| 23 | + <ActionButton |
| 24 | + label="Update numberState" |
| 25 | + onClick={() => { |
| 26 | + const s = Number.parseFloat(prompt("Value?") || ""); |
| 27 | + setNumberState(s); |
| 28 | + }} |
| 29 | + /> |
| 30 | + </li> |
| 31 | + <li> |
| 32 | + <ActionButton |
| 33 | + label="Update stringState" |
| 34 | + onClick={() => { |
| 35 | + const s = prompt("Value?") || ""; |
| 36 | + setStringState(s); |
| 37 | + }} |
| 38 | + /> |
| 39 | + </li> |
| 40 | + <li> |
| 41 | + <ActionButton |
| 42 | + label="Update objectState" |
| 43 | + onClick={() => { |
| 44 | + const s = prompt("Value?") || ""; |
| 45 | + setObjectState({ insideVal: s }); |
| 46 | + }} |
| 47 | + /> |
| 48 | + </li> |
| 49 | + <li> |
| 50 | + <ActionButton |
| 51 | + label="Do something stupid v1" |
| 52 | + onClick={() => { |
| 53 | + const s = prompt("Value?") || ""; |
| 54 | + objectState.insideVal = s; |
| 55 | + }} |
| 56 | + /> |
| 57 | + </li> |
| 58 | + <li> |
| 59 | + <ActionButton |
| 60 | + label="Do something stupid v2" |
| 61 | + onClick={() => { |
| 62 | + const s = prompt("Value?") || ""; |
| 63 | + objectState.insideVal = s; |
| 64 | + setObjectState(objectState); |
| 65 | + }} |
| 66 | + /> |
| 67 | + </li> |
| 68 | + </ul> |
| 69 | + </> |
| 70 | + ); |
| 71 | +}; |
| 72 | + |
| 73 | +export default ExampleUseState102; |
0 commit comments