2016-10-22 1 views
1

반응 팀 advises against using mixins. 그들의 suggestion for utility methods은 그들을 모듈에 넣고 가져 오는 것입니다. 이 작업을 수행하게되어 기쁩니다. 그러나 예제에서는 this이 필요할 때이를 수행하는 방법을 보여주지 않습니다.ES6 React Components에서 방법을 재사용하는 데 권장되는 방법은 무엇입니까?

따라서, 나는 나 자신을 끊임없이이 방법

updateState = updates => { 
    this.setState(state => update(state, updates)); 
}; 

그리고 내 모든 구성 요소에 몇 가지 다른 복사를 찾을 수 있습니다. "메소드"를 재사용 할 수있는 더 나은 비 비난 방법이 있습니까?

"방법"이란 this을 사용하는 비 정적 기능을 의미합니다.

답변

1

ES7 클래스 속성learn more

// updateState.js 
    function updateState() { 
     this.setState(state => update(state, updates)); 
    }; 

    // App.js 
    import updateState from './updateState.js' 

    class App extends Compnent { 
     updateState = updateState.bind(this); 
    } 
관련 문제