如何在React Native中动态渲染元素包装器

时间:2020-02-11 01:40:48

标签: react-native

假设我的包装组件A,B,C .....

过多

有时候我想渲染

    <A>
      <Text>{this.state.myText}</Text>
   </A>

还有一些我想渲染

   <B>
      <Text>{this.state.myText}</Text>
   </B>

....

....

....

如何实现这样的目标?

{this.bringTheRightComponentTag(this.state.wrapperComponent)}
          <Text>{this.state.myText}</Text>
{this.bringTheRightComponentClosingTag(this.state.wrapperComponent)}

这是一个最小化的示例。我只需要学习逻辑。

1 个答案:

答案 0 :(得分:2)

React实际上允许您在运行时(Docs here)期间将组件分配给变量。 所以你可以做类似的事情,

  const SelectedComponent = true ? A : B

  return (
    <SelectedComponent>
      {this.state.myText}
    </SelectedComponent>
  )