我一直在试图纠正我的代码中的任何语法错误,但仍继续得到“setState在现有状态期间无法更新”的错误。我已经提到类似的线索,但答案似乎并不适用于我的情况(或解决方案失败了)。setState在现有状态转换期间无法更新(尽管语法正确)
这里是我的目标是有问题的代码:
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();
},
尤其是代码挑出函数的调用在渲染和开始搜索功能在这里:
任何帮助将不胜感激。
你是指startSearch函数吗?如果是这样,我的状态设置为true –
哦 - 我看到了 - 是的,我改变了这一点,但不幸的是没有运气 –
其他尝试像这样设置:this.setState({searching: !搜索});这两种情况 –