Answer the question
In order to leave comments, you need to log in
How to properly bind to this in react?
import React from 'react';
import Counter from './components/Counter';
class App extends React.Component {
state = {
counter: 0,
};
render() {
return (
<main>
<h1>React Camp</h1>
<Counter
title='Simple counter'
counter={this.state.counter}
handleIncrement={this.handleIncrement}
/>
</main>
);
}
// ver 1
// handleIncrement() {
// this.setState((state) => ({ counter: state.counter + 1 }));
// }
// ver 2
// handleIncrement = () =>
// this.setState((state) => ({ counter: state.counter + 1 }));
}
export default App;
Answer the question
In order to leave comments, you need to log in
Switch to functions, less code, convenient. Especially now there are hooks.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question