Guest User

Untitled

a guest
Jan 25th, 2023
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState, useEffect, useMemo } from 'react';
  2.  
  3. function Counter() {
  4.  // 1. Initialize the count to 0 and create a function to increment it
  5.  const [count, setCount] = useState(0);
  6.  
  7.  // 2. Use the useEffect hook to log a message every time the count is updated
  8.  useEffect(() => {
  9.    console.log(`Count is now ${count}`);
  10.  }, [count]);
  11.  
  12.  // 3.Fill in with the function to multiply count by and return that value
  13.  const double = useMemo(() => count * 2, [count]);
  14.  
  15.  return (
  16.    <div>
  17.     {/* 4. Add  two paragraphs here displaying the current count and the double count*/ }
  18.      <p>Current count: {count}</p>
  19.      <p>Double of count: {double}</p>
  20.  
  21.      {/* 5. Set the onclick method to a function that adds one to the count by calling setCount*/ }
  22.      <button onClick={() => setCount(count + 1)}>Increment</button>
  23.    </div>
  24.  );
  25. }
  26.  
  27. export default Counter;
Advertisement
Add Comment
Please, Sign In to add comment