【发布时间】:2016-10-10 21:27:28
【问题描述】:
我一直在努力纠正代码中的任何语法错误,但我仍然收到“setState Cannot Update during Existing State”错误。我已经参考了类似的主题,但答案似乎并没有很好地适用于我的情况(或者解决方案失败了)。
这是我定位为有问题的代码:
getInitialState: function() {
return {
searching: false,
anim: new Animated.Value(305),
user: null,
isLoaded: false,
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2,
}),
}
},
render: function() {
return <View>
<View style={styles.mainView}>
{ this.renderProperElement() }
</View>
},
renderProperElement: function() {
var that = this;
return <Animated.View
style={[styles.searchWrapper, {transform: [{translateX: this.state.anim}]}]}>
<TouchableOpacity style={styles.icon} onPress={that.startSearch()}>
<Image style={styles.icon} source={require('../img/search.png')} />
</TouchableOpacity>
<TextInput style={styles.searchInput} autoFocus = {true} />
<TouchableOpacity onPress={that.stopSearch()} style={styles.icon}>
<Image style={styles.icon} source={require('../img/cancel.png')} />
</TouchableOpacity>
</Animated.View>
},
startSearch: function() {
this.setState({ searching: true });
Animated.timing(
this.state.anim,
{
toValue: 0,
easing: Easing.elastic(1),
}
).start();
},
stopSearch: function() {
Animated.timing(
this.state.anim,
{
toValue: 305,
easing: Easing.elastic(1),
}
).start();
},
特别是这里的代码单挑出了render中函数的调用和startSearch函数:
任何帮助将不胜感激。
【问题讨论】:
标签: react-native render state setstate