Advertisement
STANAANDREY

react-lea-1

Apr 14th, 2021
591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from "react";
  2. import "./style.css";
  3.  
  4. export default function App() {
  5.   const [cnt, setCnt] = React.useState(function() {
  6.     return Math.floor(Math.random(1) * 10);
  7.   });
  8.   const [k, setK] = React.useState(() => 20);
  9.   React.useEffect(
  10.     function() {
  11.       console.log("rendered");
  12.     },
  13.     [k]//optional
  14.   );
  15.   function increment() {
  16.     setCnt(function(prevCnt) {
  17.       return prevCnt + 1;
  18.     });
  19.   }
  20.   function decrement() {
  21.     setCnt(function(prevCnt) {
  22.       return prevCnt - 1;
  23.     });
  24.   }
  25.   function modK() {
  26.     setK(pk => pk + 1);
  27.   }
  28.  
  29.   return (
  30.     <React.Fragment>
  31.       <button onClick={decrement}>-</button>
  32.       <b>{cnt}</b>
  33.       <button onClick={increment}>+</button>
  34.       <br />
  35.       <b>{k}</b>
  36.       <button onClick={modK}>mod k</button>
  37.     </React.Fragment>
  38.   );
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement