In my render method I'd like to display one of two View
ponents depending on a conditional that's in my props
. e.g:
render() {
return(
<View>
<View>View A</View>
<View>View B</View>
</View>
);
}
Where the conditional is this.props.myConditional
. If false
, show A, if true
, show B...
In my render method I'd like to display one of two View
ponents depending on a conditional that's in my props
. e.g:
render() {
return(
<View>
<View>View A</View>
<View>View B</View>
</View>
);
}
Where the conditional is this.props.myConditional
. If false
, show A, if true
, show B...
3 Answers
Reset to default 11It's actually pretty easy
render() {
return <View>
{this.props.myConditional ? <View>View B</View> : <View>View A</View>}
</View>
}
If you are Using Functional Component, The latest Approach then its much more easy as,
return (
<View style={Condition ? Styles.A : Styles.B}>
</View>
)
const Styles = StyleSheet.create({ A: { display:"none" }, B:{ display:"flex" } } )
Rest you have to look into functional ponent
There are multiple ways to display based on condition:
Inline Condition
{ (link) && <Text>Read More</Text> }
Inline Condition
{ (link) ? <Text>Read More</Text> : <Text>Thank you</Text> }
Using a function
const Displaylink = (props) => { const isLink = props.isLink; if (isLnk) { return <Text>Read More</Text>; } return <Text>Thank you</Text>; }