2016-06-10 69 views
0

我一直在試圖糾正我的代碼中的任何語法錯誤,但仍繼續得到「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(); 
    }, 

尤其是代碼挑出函數的調用在渲染和開始搜索功能在這裏:

enter image description here

任何幫助將不勝感激。

回答

0

你設置你的狀態,以搜索:真當你第一次按下觸摸的不透明度,但你從來沒有改變對停止搜索方法的狀態,所以你再更好的搜索狀態更改爲false停止搜索方法爲:

 this.setState({ searching: false }); 

因此,當您再次設置此搜索狀態時,您的應用程序將導致您所需的條件。

由於嘗試使用與之前相同的值更改狀態條件,可能會發生此錯誤。

+0

你是指startSearch函數嗎?如果是這樣,我的狀態設置爲true –

+0

哦 - 我看到了 - 是的,我改變了這一點,但不幸的是沒有運氣 –

+0

其他嘗試像這樣設置:this.setState({searching: !搜索});這兩種情況 –

相關問題