I am now studying the syntax Classfor creating React components and notice that now I need to declare methods like this:
Class
class Foo extends React.Component { ... bar = () => { this.setState({ ... }) } }
instead of this:
class Foo extends React.Component { ... bar() { this.setState({ ... }) // won't work } }
or i can't use this.setState().
this.setState()
Can someone explain what the difference is between creating similar methods and how they relate to the function prototype?
, , Stage 3 , , . ( ), (, , this). :
this
class Foo extends React.component { constructor() { this.bar = () => { this.setState({ ... }) }; } }
- , prototype, "" this (, this , ).
prototype
this : , bar , this ( , ); this ( , , ).
bar
:
class Foo extends React.component { ... bar() { this.setState({ ... }) // won't work } }
this , :
class Foo extends React.component { constructor(props) { super(props); this.bar = this.bar.bind(this); } bar() { this.setState({ ... }) // works } }
Source: https://habr.com/ru/post/1689442/More articles:ReactorNettyWebSocketClient Usage Examples - spring-bootThrow an exception ObservableCollection - exceptionCopyMemory crashes Excel application - vbaiPhone-X - How to get the user to double-scroll the home indicator to go to the main screen - iosWhy is the size of the same identifier different in C and C ++? - c ++How to make GCC warn about using a class function? - c ++https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1689444/java-gc-runs-too-often-even-if-huge-memory-is-available&usg=ALkJrhh6mJZLoPRvDgumlcHaTAX7J62Lkgcompose () vs. transform () vs. as () vs. map () in Flux and Mono - reactive-programmingFind two rectangles with minimal areas that span all points - algorithmSaving an EF instance with multiple parents - c #All Articles