问题1:
问题描述
[mobx] There are multiple mobx instances active. This might lead to unexpected results: See https://github.com/mobxjs/mobx/issues/1082 for details.
重现过程
我在使用mobx的时候,还用了react-native-router-flux,而后者中就有mobx,和我自己导入的mobx版本还不一致,react-native-router-flux使用的是旧版本的,我自己导入的是最新的版本的,RN只能接受一个实例,所以就导致了这个问题。
解决方案
在package.json中,添加
"resolutions": {
"mobx": "^4.1.0",
"mobx-react": "^5.0.0"
}
在命令行中
yarn install
react-native run-android
即可
参考来源
问题2:
问题描述
Warning: setState(...): Cannot update during an existing state transition (such as within
render
or another component's constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved tocomponentWillMount
.
意思是说:不能再render或者其他组件的构造方法里中使用setState
重现过程
render() {
……
let item = this.props.item;
//我在这里使用了setState,导致了无限循环
this.setState({
praiseCount : item.post.praise_count
});
……
return (
……
)
}
解决方案
结合RN的生命周期,将这部分放到componentWillMount中去
componentWillMount() {
let item = this.props.item;
this.setState({
praiseCount : item.post.praise_count
});
}
问题3:
问题描述
TextInput中使用value={this.props.value},当value发生变化的时候,TextInput的这个值并没有改变
出现原因
React受控与非受控组件