如何渲染作为Prop发送的组件

时间:2017-12-19 09:22:24

标签: reactjs react-native

我有一个像这样的组件     <A componentAsProps={<Text>Some Text</Text>} />

如何在render方法中呈现componentAsProps

3 个答案:

答案 0 :(得分:4)

你可以这样做:

renderChildCompo = () => <Text>Some text</Text>;

render() {
 return (
   <A componentAsProps={this.renderChildCompo} />
 );
}
// ...

在你的./A/index.js文件中

render() {
 return (
  {this.props.componentAsProps()}
 );
}

答案 1 :(得分:1)

您可以将组件作为子项传递:

父组件:

<ChildComponent>
     <Text>Some Text</Text>
</ChildComponent>

子组件:

class ChildComponent extends Component {
    render() {
        return (
            <View>
                {this.props.children}
            </View>
        );
    }
}

你可以传递一个值作为道具并像这样使用它:

父组件:

<ChildComponent text="Some Text" />

子组件:

<Text>{this.props.text}</Text>

答案 2 :(得分:0)

我找到了所有我需要做的答案     {this.props.componentAsProps} 在子组件的render方法中