Reputation:
In my render method I'd like to display one of two View
components 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...
Upvotes: 3
Views: 10413
Reputation: 353
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>;
}
Upvotes: 0
Reputation: 61
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 component
Upvotes: 3
Reputation: 8552
It's actually pretty easy
render() {
return <View>
{this.props.myConditional ? <View>View B</View> : <View>View A</View>}
</View>
}
Upvotes: 11