javascript - React Native 将组件传递给组件
问题描述
我是本机反应的新手,不确定如何在组件之间传递组件。
我创建了一组能够生成表格的函数。我试图通过允许entryData
成为组件以及字符串或数字类型来使表格更通用。
如果useTextWrapper
为真且类型entryData
为数字或字符串,则表格会将数据放入文本组件中。否则,我假设它entryData
必须是一个组件,然后我只想entryData
在视图组件中解包。这是我到目前为止所尝试的,我得到一个“必须在文本组件中呈现文本字符串”。有没有办法做我想做的事?
function EntryDataWrapper(props) {
return <View> {props.children} </View>;
}
function RowEntry(props) {
let {entryStyle, entryTextStyle, entryData, useTextWrapper} = props;
const theType = typeof entryData;
return (
<View style={entryStyle}>
{useTextWrapper && (theType == 'nuber' || theType == 'string') ? (
<Text style={entryTextStyle}>{entryData}</Text>
) : (
<EntryDataWrapper> {entryData} </EntryDataWrapper>
)}
</View>
);
}
解决方案
错误消息已经说明了问题。 Text strings must be rendered within text components
. EntryDataWrapper 组件没有这样做。
因此,您应该将 Text 组件添加到其中。请按照下面的代码。
function EntryDataWrapper(props) {
return <View> <Text>{props.children}</Text> </View>;
}
function RowEntry(props) {
let {entryStyle, entryTextStyle, entryData, useTextWrapper} = props;
const theType = typeof entryData;
return (
<View style={entryStyle}>
{useTextWrapper && (theType == 'nuber' || theType == 'string') ? (
<Text style={entryTextStyle}>{entryData}</Text>
) : (
<EntryDataWrapper> {entryData} </EntryDataWrapper>
)}
</View>
);
}
欢呼!
推荐阅读
- jolt - 动态键需要 Jolt Spec
- hadoop - 所有hadoop数据节点都在运行,但是“数据节点信息”说只有一个数据节点在运行
- swift - 滑动删除重新出现在收藏选项卡中的项目
- c++ - 为什么这个列表初始化有效,而另一个无效?
- javascript - 为什么 let 在 javaScript 中类型转换为字符串时不被识别为关键字?
- python - 根据与谁兼容来匹配组中人员的算法
- spring-security - Spring oauh2 - 如何保存 CustomOAuth2UserService/CustomOidcUserService 中的自定义文件
- c# - Visual Studio 2019 中是否有可用的 ToString() 生成器?
- sql - SQL 条件计数
- reactjs - 打字稿错误类型'字符串| undefined' 不满足约束 'string | 号码 | 象征'