react use setstate code example
Example 1: setstate in react
Functional Component
const [counter, setCounter] = useState(0);
setCouter(counter + 1);
Example 2: reading state react
class Example extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
}
Example 3: react setState
incrementCount() {
this.setState({count: this.state.count + 1});
}
handleSomething() {
this.incrementCount();
this.incrementCount();
this.incrementCount();
}
Example 4: set state
setState({ searchTerm: event.target.value })
Example 5: reading state react
<p>You clicked {count} times</p>