Created
November 24, 2020 19:30
-
-
Save indiesquidge/579e56e547ce9828ca8dad60df13e900 to your computer and use it in GitHub Desktop.
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 characters
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 characters
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> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment