React native 反应本原中的三元算子
我只需要在变量为true时显示一个组件,基本上我将创建两个按钮,一个将变量设置为false,另一个设置为true。我试着使用角度的概念。我需要这样的东西:React native 反应本原中的三元算子,react-native,React Native,我只需要在变量为true时显示一个组件,基本上我将创建两个按钮,一个将变量设置为false,另一个设置为true。我试着使用角度的概念。我需要这样的东西: render() { return ( <View> <Button title="Click me" onPress={ () => { this.loading = true } } /> {this.loading ? <Modal /> : null}
render() {
return (
<View>
<Button
title="Click me"
onPress={ () => { this.loading = true } }
/>
{this.loading ? <Modal /> : null}
</View>
);
}
render(){
返回(
{this.loading=true}}
/>
{this.loading?:null}
);
}
您似乎是新来的React,处于React状态,处理程序要么处于状态,要么通过了道具
您可以使用组件状态(如show
)来实现这一点,使用设置状态的单击处理程序,然后在渲染中,您可以检查此.state.show
,并决定是否显示组件
setShow = () = >{
this.setstate({show : true});
}
render() {
return (
<View>
<Button
title="Click me"
onPress={this.setShow}
/>
{this.state.show ? <Modal /> : null}
</View>
);
}
setShow=()=>{
this.setstate({show:true});
}
render(){
返回(
{this.state.show?:null}
);
}
使用状态变量重新渲染受影响的数据您需要将该变量设置为状态,否则当值更改时视图不会重新渲染。此外,常见的约定是{this.loading&&}
它可以工作,但我必须将按钮更改为可触摸按钮它可以工作,但我必须将按钮更改为可触摸按钮,我使用{this.loading&}