[英]React Native Conditional Rendering without if statement
Is it possible to re-render specific components without using if/else statement in the rendersection. 是否可以在不使用render / else语句的情况下重新渲染特定的组件。 So when a specific statement changed only his specific component(s) will re-render while the rest remain intact.
因此,当特定语句更改时,只有他的特定组件会重新呈现,而其余组件则保持不变。
Because if I use the componentWillUpdate or shouldComponentUpdate it will re-render the whole app scene again. 因为如果我使用componentWillUpdate或shouldComponentUpdate,它将再次重新渲染整个应用程序场景。
I look forward to your answers. 期待您的答复。
You can try something like - 您可以尝试-
class MainComponent extends React.Component {
displayMessage() {
if (this.props.isGreeting) {
return <Text> Hello, JSX! </Text>;
} else {
return <Text> Goodbye, JSX! </Text>;
}
}
render() {
return ( <View> { this.displayMessage() } </View> );
}
}
Check this article - https://medium.com/@szholdiyarov/conditional-rendering-in-react-native-286351816db4 查看本文-https://medium.com/@szholdiyarov/conditional-rendering-in-react-native-286351816db4
You can try with new ES6 enhanced object litrals. 您可以尝试使用新的ES6增强对象库。 We can access the property of an object using bracket
[]
notation: 我们可以使用方括号
[]
表示法来访问对象的属性:
myObj = { "name":"Stan", "age":26, "car": "Lamborghini"};
x = myObj["name"]; //x will contain Stan
So you can use this approach for conditional rendering 因此您可以使用这种方法进行条件渲染
this.state = {
contentToDisplay: "content1",
}
render() {
return (
<section>
{{
content1: <Content1 />,
content2: <Content2 />,
content3: <Content3 />,
}[this.state.contentToDisplay]}
</section>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.