Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating wrapper component

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.

like image 474
Sinan Samet Avatar asked Oct 18 '16 14:10

Sinan Samet


People also ask

How do you make a wrapper component?

To create wrapper components, you'll first learn to use the rest and spread operators to collect unused props to pass down to nested components. Then you'll create a component that uses the built-in children component to wrap nested components in JSX as if they were HTML elements.

What is wrapped component?

The wrapped component receives all the props of the container, along with a new prop, data , which it uses to render its output. The HOC isn't concerned with how or why the data is used, and the wrapped component isn't concerned with where the data came from.

What is JSX wrapper?

It is called JSX, and it is a syntax extension to JavaScript. We recommend using it with React to describe what the UI should look like. JSX may remind you of a template language, but it comes with the full power of JavaScript. JSX produces React “elements”.

What is wrapper component in angular?

A custom component that takes the type of a component as input and creates an instance of that component type inside itself. When the input is updated, the previously added dynamic component is removed and the new one added instead.


1 Answers

You can create the wrapper using props.children

Functional component

const WrapperComponent = ({ children }) => (
  <Image source={someImage}>
    {children}
  </Image>
);

Class component

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.

like image 101
Ori Drori Avatar answered Sep 27 '22 01:09

Ori Drori