클래스 함수를 클래스 외부로 이동하고 React에서 this 키워드를 사용하는 방법은 무엇입니까?

lw19901

반응에서 다음과 같은 코드가 있습니다.

export class Foo extends React.Component {
 function_name() {
   this.setState({blah: 'bleh'})
  }
 this.function_name()
}

하지만 정말 이렇게 작동하고 싶습니다

function_name() {
 this.setState({blah: 'bleh'})
}

export class Foo extends React.Component {
 this.function_name()
}

이런 종류의 작업을 수행 할 수있는 방법이 있습니까? 기본적으로 클래스 외부의 함수가 들여 쓰기를 줄이기를 원합니다. 문제는 내가 얻는 것 function_name is not a function입니다.

확실한 성능

클래스를 선언 / 내 보낸 후 프로토 타입에 할당 할 수 있습니다.

export class Foo extends React.Component {
  this.function_name()
}
Foo.prototype.function_name = function() {
  this.setState({blah: 'bleh'})
};

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관