Skip to content

Instantly share code, notes, and snippets.

@LucasCalazans
Last active March 17, 2023 13:27
Show Gist options
  • Save LucasCalazans/7951d5ab1f308162c627a2f2d7adc3ba to your computer and use it in GitHub Desktop.
Save LucasCalazans/7951d5ab1f308162c627a2f2d7adc3ba to your computer and use it in GitHub Desktop.
Comparation with React Hooks
// Usando classe
class Counter extends Component {
state = {
counter: 1,
}
incrementCounter = () => {
this.setState(({ counter }) => ({ counter: counter + 1 }));
};
render() {
return (
<button onClick={this.incrementCounter}>{this.state.counter}</button>
)
}
}
// Mesma funcionalidade usando Hooks
const Counter = () => {
const [counter, setCounter] = useState(1)
return (
<button onClick={() => setCounter(counter + 1)}>{counter}</button>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment