Вот пример кода
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
counter: this.props.counter
}
incCounter = () => {
if (this.state.counter < 50) {
this.setState(state => ({
counter: state.counter + 1
}))
}
}
Почему здесь this ссылается на App или тут
resetCounter = () => {
this.setState({
counter: this.props.counter
})
}
Как работает это правило? Эти функции находятся не внутри App но всё равно ссылаются на него.
App? Они внутри же. – CrazyElf Oct 13 '23 at 10:41JavaScriptне очень знаю, но судя по всему так. Это же всё внутри описания классаAppпроисходит. – CrazyElf Oct 13 '23 at 10:54thisв JS... https://doka.guide/js/function-context/ – ksa Oct 13 '23 at 11:25incCounterнаходится очень даже внутри классаApp– andreymal Oct 13 '23 at 12:20incCounterвообще внутри конструктора находится… – Alexey Ten Oct 13 '23 at 16:53