首页 > 解决方案 > 没有 if 语句的 React Native Conditional Rendering

问题描述

是否可以在不使用渲染部分中的 if/else 语句的情况下重新渲染特定组件。因此,当特定语句更改时,只有他的特定组件将重新渲染,而其余部分保持不变。

因为如果我使用 componentWillUpdate 或 shouldComponentUpdate 它将再次重新渲染整个应用场景。

我期待着你的回答。

标签: javascriptandroidreact-nativerendering

解决方案


你可以尝试类似的东西 -

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> );
        }
    }

检查这篇文章 - https://medium.com/@szholdiyarov/conditional-rendering-in-react-native-286351816db4


推荐阅读