2017-01-17 36 views
0

我想在應用程序中狀態改變時觸發一個函數。我有一個模式改變狀態的按鈕。React Native在狀態改變時執行一個函數

我家成分(編輯)

... 
constructor(props) { 
super(props); 
    this.state = { 
    color:'red', 
    modalVisible: false, 
    } 
} 
... 
render() { 
return (
    <View> 

     <Modal 
     animationType={"slide"} 
     transparent={false} 
     visible={this.state.modalVisible} 
     > 
     <View style={{marginTop: 22}}> 
     <TouchableOpacity onPress={()=>this.setState({color:'blue'})}> 
      <Text>Blue</Text> 
     <TouchableOpacity> 
     <TouchableOpacity onPress={()=>this.setModalVisible(false)}> 
      <Text>Blue</Text> 
     <TouchableOpacity> 
     </View> 
    </Modal> 

    <Text style={{color:this.state.color}}> 
    Foo 
    </Text> 
    </View> 
) 
... 

但是當我關閉模式,我的文字的顏色不會更改爲藍色。它保持紅色,直到我打開模式備份,然後再次關閉。它像'顏色'的狀態沒有被更新。在使用redux和'componentWillReceiveProps(nextProps)'之前,我碰到過這個問題,但是這讓我陷入了同一個組件。

我正在使用文檔中的原生模態組件。 https://facebook.github.io/react-native/docs/modal.html

+0

你能發佈更多的代碼,以顯示你的兩個片段是如何關聯? – ArneHugo

+0

我編輯了我的代碼以反映它們如何相關。 – gbland777

+0

你看過@ fumi_hwh回答中的[link](https://rnplay.org/apps/fDGs3A)嗎?似乎工作給我。如果它不能幫助您解決問題,請嘗試在[rnplay.org](https://rnplay.org)中創建一個可運行的示例,以重現您的問題。 (編輯:[修改他的例子](https://rnplay.org/apps/rKeOYg)更像你的。) – ArneHugo

回答

相關問題