コード内の構文エラーを修正しようとして頭を壁にぶつけていますが、「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();
},
特に、コードは次のように render 内の関数の呼び出しと startSearch 関数を選択しています。
どんな助けでも大歓迎です。