2017-05-22 10 views
0

React + Mobx 앱을 제작하고 있는데 제 3 자지도 API를 기반으로하기 때문에 아무 것도 렌더링하지 않는 구성 요소가있는 상황이 있습니다. 일부 상점 속성 변경에 대응해야합니다.mobx를 사용하여 소품을 렌더링하지 않고 반응하는 방법

componentDidMount() { 
    mapApi.doSomething(this.props.store.value) 
} 

componentWillReact() { 
    mapApi.doSomething(this.props.store.value) 
} 

render() { 
    //workaround to make componentWillReact trigger 
    console.log(this.props.store.value) 
    return null 
} 

세련된 구현 방법이 있습니까? 내가 제대로 이해하면

답변

1

를 사용할 수 있습니다, 당신은 함수 렌더링을 사용하지 무언가에 반응합니다. 당신이 뭔가를 할 수 있습니다

@observer 
class ListView extends React.Component { 
    constructor(props) { 
     super(props) 
    } 

@observable filterType = 'all' 

handleFilterTypeChange(filterType) { 
    fetch('http://endpoint/according/to/filtertype') 
    .then() 
    .then() 
} 

    // I want the method that autoruns whenever the @observable filterType value changes 
hookThatDetectsWhenObservableValueChanges = reaction(
    () => this.filterType, 
    filterType => { 
     this.handleFilterTypeChange(filterType); 
    } 
) 
} 

을이 솔루션은 https://github.com/mobxjs/mobx-react/issues/122#issuecomment-246358153

여기에 언급