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.
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.
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.
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”.
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.
You can create the wrapper using props.children
const WrapperComponent = ({ children }) => (
<Image source={someImage}>
{children}
</Image>
);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With