Reactjs 如何在不使用mobx渲染的情况下对道具更改作出反应
我正在构建一个React+Mobx应用程序,我有一个组件不呈现任何内容,因为它都基于第三方地图API,但我需要对一些商店属性更改做出反应:Reactjs 如何在不使用mobx渲染的情况下对道具更改作出反应,reactjs,mobx,mobx-react,Reactjs,Mobx,Mobx React,我正在构建一个React+Mobx应用程序,我有一个组件不呈现任何内容,因为它都基于第三方地图API,但我需要对一些商店属性更改做出反应: componentDidMount(){ mapApi.doSomething(this.props.store.value) } 组件将反应(){ mapApi.doSomething(this.props.store.value) } 渲染(){ //使componentWillReact触发器 console.log(this.props.store.
componentDidMount(){
mapApi.doSomething(this.props.store.value)
}
组件将反应(){
mapApi.doSomething(this.props.store.value)
}
渲染(){
//使componentWillReact触发器
console.log(this.props.store.value)
返回空
}
有没有一种优雅的方法来实现这一点?您可以使用
如果我理解正确,您希望对渲染函数中未使用的内容做出反应。您可以这样做:
@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);
}
)
}
这里提到了这个解决方案
@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);
}
)
}