close
答:
1. state設一個變數存true / false
2. 把if else判斷獨立成一個function,裏頭判斷變數是true就return內容,false就return null。
3. 在render的return中呼叫這個function,要不要顯示元件,控制state這個變數就好。
--
I would do something like this:
var myComponent = React.createComponent({
getInitialState: function () {
return {
showCancel: false,
};
},
toggleCancel: function () {
this.setState({
showCancel: !this.state.showCancel
});
}
_renderCancel: function () {
if (this.state.showCancel) {
return (
<TouchableHighlight
onPress={this.toggleCancel()}>
<View>
<Text style={styles.cancelButtonText}>Cancel</Text>
</View>
</TouchableHighlight>
);
} else {
return null;
}
},
render: function () {
return (
<TextInput
onFocus={this.toggleCancel()}
onChangeText={(text) => this.doSearch({input: text})} />
{this._renderCancel()}
);
}
});
轉自 https://stackoverflow.com/questions/30266831/hide-show-components-in-react-native
--
--
全站熱搜
留言列表