Advertisement
Guest User

Untitled

a guest
Sep 27th, 2020
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState } from 'react';
  2. import './App.css';
  3.  
  4. function computeInitialCounter () {
  5.   console.log('Computing counter value');
  6.  
  7.   // simulating calculations
  8.   return Math.trunc(Math.random() * 20)
  9. }
  10.  
  11. function App() {
  12.   const [counter, setCounter] = useState(() => {
  13.     return computeInitialCounter
  14.   });
  15.  
  16.   function increment () {
  17.     setCounter(counter + 1);
  18.   };
  19.  
  20.   function decrement () {
  21.     setCounter(counter - 1);
  22.   };
  23.  
  24.   return (
  25.    <div>
  26.      <h1>Счетчик: {counter}</h1>
  27.      <button onClick={increment}>Добавить</button>
  28.      <button onClick={decrement}>Убрать</button>
  29.    </div>
  30.   );
  31. }
  32.  
  33. export default App;
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement