我有一个问题,当我在组件中更改状态时,react原生如何决定完全跳过重新呈现。
我有一个下拉菜单,当它的一个选项被选择时,它将加载下一个下拉菜单的选项。在本例中: country和city。我现在想做的就是在选择一个国家时,在城市下拉菜单下显示“加载”文本。下面是我的代码片段:
onCountryChange(country) {
this.setState({loadingCity: true});
//looping country array here to get the list of city option
this.setState({loadingCity: false});
}
render () {
return (
<View>
<Text>Country</Text>
<View>
<Dropdown
style={{ height: 20, width: 300}} values={this.state.country_name}
selected={0} onChange={this.onCountryChange.bind(this)}/>
</View>
</View>
<View>
<Text>City</Text>
{ this.state.selectedCountry != '' ?
<View>
<Dropdown
style={{ height: 20, width: 300}} values={this.state.city_name}
selected={0} onChange={this.onCityChange.bind(this)}/>
</View>
:
<Text>Please pick a country first</Text>
}
{ this.state.loadingCity?
<View>
<View>
<Image
style={{height: 10, width: 10}}
source={require('./images/loading.gif')} />
</View>
<Text>Loading</Text>
</View>
:
null
}
</View>
)
}我尝试删除代码行this.setState({loadingCountry: false}),但它实际上显示了加载文本。但是很明显,当我重新添加这行代码时,react原生代码完全跳过了与loadingCountry相关的呈现。有没有办法强制加载出现?我已经尝试过使用this.forceUpdate(),但是它仍然不起作用。
更新:这是我尝试使用Timer Mixin做的事情:
onCountryChange(country) {
this.setState({loadingCity: true});
//looping country array here to get the list of city option
this.setTimeout(this.setState({loadingCity: false}),100000);
}但是由于某些原因,setTimeout实际上并没有等待100秒来触发this.setState({loadingCity: false})函数。
发布于 2016-02-10 08:54:13
在同一同步代码块中调用setState({loadingCity: true}),然后调用setState({loadingCity: false})将不会呈现。即使是这样,它也可能会比你看到的更快。
如果希望加载指示器保持不变,请不要将其设置为false。
如果您只想让它在几秒钟内可见,那么可以将其封装在setTimeout中(不过不要忘记使用setState({loadingCity: false}) -timer-mixin)。
https://stackoverflow.com/questions/35296780
复制相似问题