Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如果道具是组件,如何渲染组件?_Reactjs_React Native_React Props_React Component - Fatal编程技术网

Reactjs 如果道具是组件,如何渲染组件?

Reactjs 如果道具是组件,如何渲染组件?,reactjs,react-native,react-props,react-component,Reactjs,React Native,React Props,React Component,我想制作一个“灵活”的组件,但不确定这是否是一个好代码: { leftText instanceof Object ? leftText : <Text> { leftText } </Text> } { 对象的leftText实例? 左文本: {leftText} } 因此,如果我收到一个组件作为道具,我不想把它包装成另一个组件,而是渲染组件本身。否则只需要将道具包装在

我想制作一个“灵活”的组件,但不确定这是否是一个好代码:

{
    leftText instanceof Object ? 
        leftText :
           <Text>
              { leftText }
           </Text>
}
{
对象的leftText实例?
左文本:
{leftText}
}
因此,如果我收到一个组件作为道具,我不想把它包装成另一个组件,而是渲染组件本身。否则只需要将道具包装在文本组件中


有解决这个问题的好方法吗?

是的,你的方法确实有效!但是仅仅验证子对象是否是对象是非常通用的。您可以使用:

{
    React.IsValidElement(leftText) ? 
        leftText :
           <Text>
              { leftText }
           </Text>
}
{
React.IsValidElement(左文本)?
左文本:
{leftText}
}

使用怎么样?是的,这一个看起来肯定比实例要好得多。谢谢你的提醒!!你说得对,这个看起来更清晰。谢谢