Creating wrapper component

家住魔仙堡 提交于 2019-12-22 04:09:46

问题


I want to create a wrapper component like this:

Wrapper Component:

class WrapperComponent extends Component {

  render() {
    return (
      <Image source={someImage}>
          <App />
      </Image>);
  }
}

App:

class App extends Component {

  render() {
    return (
      <WrapperComponent>
        <Text>Some text</Text>
      </WrapperComponent>
  }
}

I want to use this for the default things like a background image. Is there a way to do this? Sorry I am new in this language.


回答1:


You can create the wrapper using props.children:

class WrapperComponent extends Component {

  render() {
    return (
      <Image source={someImage}>
          { this.props.children }
      </Image>);
  }
}

Whatever you'll put inside the <WrapperComponent></WrapperComponent> tags will be rendered.



来源:https://stackoverflow.com/questions/40111292/creating-wrapper-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!