【问题标题】:React Native Passing a component to a componentReact Native 将组件传递给组件
【发布时间】:2019-01-18 08:47:47
【问题描述】:

我是反应原生的新手,我不确定如何在组件之间传递组件。

我创建了一组能够生成表格的函数。我试图通过允许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>
    );
}

【问题讨论】:

  • 对于我在代码示例中看到的内容,您在 this 线程上执行了类似的操作。检查这是否是您的问题的解决方案。

标签: javascript react-native components


【解决方案1】:

错误消息已经说明了问题。 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>
        );
    }

加油!

【讨论】:

    猜你喜欢
    • 2019-02-18
    • 2019-12-06
    • 2022-11-10
    • 2019-10-23
    • 2019-07-13
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    • 2018-12-28
    相关资源
    最近更新 更多