javascript - 使用三元表达式渲染组件

标签 javascript reactjs react-native

我去使用三元表达式渲染一个组件。

目前我正在做这样的事情

 <View style={container}>
          { this.state.loaded ? 
                            (<VictoryChart
                    theme={VictoryTheme.material}
                    >
                    <VictoryArea
                        style={{ data: { fill: "#c43a31" } }}
                        data={this.coinHistoryData} 
                        x="cHT"
                        y="cHTVU"
                        domain={{ y: [0, 30000] }}
                    />
                    </VictoryChart>)
          : (<Text> Loading..</Text>)}  </View>

但这不起作用,并抛出一个错误提示 Invariant Violation

ExceptionsManager.js:84 Unhandled JS Exception: Invariant Violation: Invariant Violation: Text strings must be rendered within a component.

[问题:]如何修复它并在三元表达式中渲染整个组件

Ps:据此stackoverflow question :当我们进行内联条件渲染时会发生这种情况。

最佳答案

我以前在 react-native 看过.
我知道有两个原因会引发此错误:

  1. 返回 null/undefined (我认为不是你的情况)
  2. </Text> 后的空格标签(我想这就是你的情况)

所以去掉最后的空格:

<View style={container}>
          { this.state.loaded ? 
                            (<VictoryChart
                    theme={VictoryTheme.material}
                    >
                    <VictoryArea
                        style={{ data: { fill: "#c43a31" } }}
                        data={this.coinHistoryData} 
                        x="cHT"
                        y="cHTVU"
                        domain={{ y: [0, 30000] }}
                    />
                    </VictoryChart>)
          : (<Text> Loading..</Text>)}  </View> //<--- spaces are here

所以这条线

: (<Text> Loading..</Text>)}  </View> 

应该是这样的

: (<Text> Loading..</Text>)}</View> 

关于javascript - 使用三元表达式渲染组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52375710/

相关文章:

javascript - XMLHttpRequest 页面卡住

reactjs - React - 将状态保存到本地存储

javascript - 使用 redux-thunk 和 redux-observable

ios - 使用FastLane自动化iOS应用

ios - react-native-firebase:onNotificationOpenedApp 和 getInitialNotification 不适用于 iOS

javascript - 正则表达式匹配错误数据

javascript - 在 jQuery 中使悬停重复 Action

reactjs - 验证 Yup 中的对象项

react-native - navigator.geolocation.getCurrentPosition 在使用 React Native 的 iOS 模拟器中未触发

javascript - 将表单中的两个值相乘