catt-wuyang / stackoverflow Goto Github PK
View Code? Open in Web Editor NEWHome Page: https://catt-wuyang.github.io/stackoverflow
License: MIT License
Home Page: https://catt-wuyang.github.io/stackoverflow
License: MIT License
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>
);
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.