GithubHelp home page GithubHelp logo

catt-wuyang / stackoverflow Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 0.0 285 KB

Home Page: https://catt-wuyang.github.io/stackoverflow

License: MIT License

TypeScript 43.31% Less 56.69%
stackoverflow-answer

stackoverflow's People

Contributors

catt-wuyang avatar

Watchers

 avatar

stackoverflow's Issues

【React】a question to learn react hooks api in-depth

import React from "react";

const Button = ({ onClick }) => {
  /*
   * Task 2. How to avoid Button rerenders?
   */
  console.log("Button rerender");

  return <button onClick={onClick}>add</button>;
});

export default function App() {
  const [counter, setCounter] = useState(0);

  /*
   * Task 1. Print to console (console.log) every 1000ms
   * current counter value.
   * Interval of printing should not be interrupted
   * by App rerenders or user interactions, e.g.
   * if user click on "add" button every 700ms, value
   * of counter should be still printing every 1000ms
   */

  return (
    <div>
      <h1>Counter: {counter}</h1>
      <Button onClick={() => setCounter(counter + 1)} />
    </div>
  );
}
import React, { useState, useEffect, useRef, useCallback, memo } from "react";

const Button = memo(({ onClick }) => {
  /*
   * Task 2. How to avoid Button rerenders?
   */
  console.log("Button rerender");

  return <button onClick={onClick}>add</button>;
});

Button.displayName = Button;

export default function App() {
  const timer = useRef();
  const printer = useRef();
  const [counter, setCounter] = useState(0);

  /*
   * Task 1. Print to console (console.log) every 1000ms
   * current counter value.
   * Interval of printing should not be interrupted
   * by App rerenders or user interactions, e.g.
   * if user click on "add" button every 700ms, value
   * of counter should be still printing every 1000ms
   */

  printer.current = () => console.log(counter);

  console.log("app rerender");

  useEffect(() => {
    timer.current = setInterval(() => printer.current(), 1000);

    return () => clearInterval(timer.current);
  }, []);

  const clickHandler = useCallback(() => {
    setCounter((counter) => counter + 1);
  }, []);

  return (
    <div>
      <h1>Counter: {counter}</h1>
      <Button onClick={clickHandler} />
    </div>
  );
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.