我必須呈現基於一些condition.Here一個touchablehighlight
是渲染方法的一部分:組件未更新動態rendring
render(){
. . .
{ !this.state.isNewMsg?
<View key={this.state.isNewMsg}>
<TouchableHighlight
<Text>
. . .
</Text>
</TouchableHighlight>
</View>
:
<View>
<TouchableHighlight>
. . .
</TouchableHighlight>
</View>
}
,我的狀態改變:
someMethod() {
this.setState({isNewMsg:true});
}
但組件不會更新。有條件的重新發布有哪些錯誤?我曾嘗試分配touchablehighlight
Id,然後使用id值更改狀態,但沒有運氣。
UPDATE
更多代碼:
Render方法:安裝
render() {
return (
. . .
{!this.state.isNewMsg?
<View key={this.state.isNewMsg} style={{ flex:1, width:200, alignSelf:'center'}}>
<TouchableHighlight underlayColor = {'white'}
onPress={this._seeMessage.bind(this)}
disabled={this.state.dontShowMsg} >
<Text style = {[styles.button,{color:this.state.buttonColor,
backgroundColor:this.state.buttonBackgroundColor}]}>
Messages
</Text>
</TouchableHighlight>
</View>
:
<View key={this.state.isNewMsg} style={{ flex:1, width:200, alignSelf:'center'}}>
<TouchableHighlight
underlayColor = {'white'}
onPress={this._seeMessage.bind(this)}
>
<Image
style={styles.button}
source={require('./newMsg.gif')}
/>
</TouchableHighlight>
</View>
}
. . .
<otherComponents/>
. . .
法呼籲組件:
getMessagesFireBase(){
var id=this.state.uid;
var that = this;
Firebase.ref('/msg/' + this.state.uid).once('value').then(
function(snapshot) {
snapshot.forEach(function(child){
var key = child.key;
var value = child.val();
if(that.isObject(value) && value.isReply)
{
if(that.state.oldMsg!==value.reply)
{
try {
AsyncStorage.setItem('oldMsg',value.reply);
this.setState({isNewMsg:true}); //not updated
} catch (error) {
// Error saving data
}
}
that.setState({userQuestion:value.umsg, //works fine
userReply:value.reply,
dontShowMsg:false,
buttonColor:'white',
buttonBackgroundColor:'rgba(128,0,0,0.3)'
});
}
});
});
}
嗯,你知道一些簡單的自殺方法嗎? –
放輕鬆:)這都是因爲JavaScript。每個人都犯過這樣的錯誤。 –
使用ESLint。它會幫助你避免這樣的錯誤。 –