Skip to content

Instantly share code, notes, and snippets.

@AZagatti
Created May 20, 2020 18:18
Show Gist options
  • Save AZagatti/f8cbceb21bd834e9816551f06ea8a122 to your computer and use it in GitHub Desktop.
Save AZagatti/f8cbceb21bd834e9816551f06ea8a122 to your computer and use it in GitHub Desktop.
import React, { PureComponent } from "react";
import "./styles.css";
export default class Class extends PureComponent {
state = {
name: "Zagatti",
count: 0
};
componentDidMount() {
document.title = `${this.state.name}'s counter`;
}
componentDidUpdate(prevProps, prevState) {
if (prevState.name !== this.state.name) {
document.title = `${this.state.name}'s counter`;
}
}
increment = () => {
this.setState(state => {
return { count: state.count + 1 };
});
};
decrement = () => {
this.setState(state => {
return { count: state.count - 1 };
});
};
updateName = e => {
this.setState({ name: e.target.value });
};
render() {
return (
<div id="section">
<div class="top">
<label>Nome:</label>
<input
type="text"
name="name"
placeholder="Seu nome"
value={this.state.name}
onChange={this.updateName}
/>
</div>
<div class="buttons">
<button onClick={this.decrement}>-</button>
<span>{this.state.count}</span>
<button onClick={this.increment}>+</button>
</div>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment