Sobes.tech
Junior — Senior

Component rerender check when clicking the second button in React

livecode

Task condition

It is necessary to find out whether the component will re-render when the user clicks the second button.

import { useCallback, useEffect, useMemo, useState } from "react";

export function App() {
  const [counter, setCounter] = useState(0);
  const [user, setUser] = useState({
    name: 'Anton',
  });

  const handleUserClick = () => {
    user.name = "Alex";
    console.log(user.name);
    setUser(user);
  };

  const increment = () => {
    setCounter(counter + 1);
  };

  const handleBtnClick = useCallback(() => {
    increment();
  }, []);

  return (
    <div>
      <button onClick={handleBtnClick}>{counter}</button>
      <button onClick={handleUserClick}>{user.name}</button>
      {/* <button onClick={btnClick2}>Compute Heavy</button> */}
      <div />
      <input type="text" />
    </div>
  );
}