Skip to content

Instantly share code, notes, and snippets.

@indiesquidge
Created November 24, 2020 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indiesquidge/579e56e547ce9828ca8dad60df13e900 to your computer and use it in GitHub Desktop.
Save indiesquidge/579e56e547ce9828ca8dad60df13e900 to your computer and use it in GitHub Desktop.
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>
);
}
}
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