Created
November 24, 2020 19:30
-
-
Save indiesquidge/579e56e547ce9828ca8dad60df13e900 to your computer and use it in GitHub Desktop.
Revisions
-
indiesquidge created this gist
Nov 24, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ export class UserList extends React.Component { state = { count: 0 }; handleClick = (item) => { setTimeout(() => { console.log(`You clicked ${this.props.group} ${this.state.count} times`); }, 3000); }; render() { return ( <div onClick={() => { this.setState({ count: this.state.count + 1 }); }} > <HugeListOfItems group={this.props.group} handleClick={this.handleClick} /> </div> ); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ export function UserList({ group }) { const [count, setCount] = React.useState(0); // Have to remember to useCallback here to avoid // the function getting recreated. const handleClick = React.useCallback( (item) => { setTimeout(() => { console.log(`You clicked ${group} ${count} times`); }, 3000); // Have to pass all of the variable references here so // React knows when to update the callback function. }, [group, count] ); return ( <div onClick={() => setCount(count + 1)}> <HugeListOfItems group={group} handleClick={handleClick} /> </div> ); }