首页 > 解决方案 > 在 react-native 中将模板文字插入视图

问题描述

我正在将一个网络应用程序移动到 React-native。我第一次接触 React 世界,所以对于一个全能的初学者来说,这是一个陡峭的学习曲线。

我的 javascript 运行良好。结果是我的 main 函数返回了一个字符串,在 javascript 中,我使用该字符串将图像动态插入到 DOM 中。我想在 React-native 中做同样的事情,但遇到了麻烦。

我从 javascript 得到的结果是这种形式:

    `<Image source={require("./images/pad.png")} style={{ width: 40, height: 318, flexDirection: "row" }} /><Image source={require("./images/banana1.png")} style={{ width: 20, height: 318, flexDirection: "row" }} /><Image source={require("./images/banana2.png")} style={{ width: 20, height: 318, flexDirection: "row" }} /><Image source={require("./images/banana3.png")} style={{ width: 20, height: 318, flexDirection: "row" }} />`

所以它具有正确路径和样式的标签。但是,当我尝试将其插入视图时,我得到一个

 "Invariant Violation: Text strings must be rendered within a <Text> component."

不是专门寻找书面解决方案,只是希望有人指出我正确的方向。我将模板文字插入到构造函数中,一切正常。

  <View>{this.state.bananasStr}</View>

我可以从那里访问它,或者从 javascript 函数作为变量访问它。

标签: javascriptstringreact-native

解决方案


推荐阅读