我一直在努力纠正代码中的任何语法错误,但我继续收到“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 函数中挑出了函数的调用:
任何帮助将不胜感激。