Let me Explain my question with an example:
in SomeComponent.js I have the following
export default class SomeComponent extends React.Component { render() { return ( <View style={{flex:1}}> </View> ) } }
and in Root.js it imports 'SomeComponent' as follow
import SomeComponent from './SomeCoponent' export default class SomeComponent extends React.Component { render() { return ( <SomeComponent> <Text> hello </Text> </SomeComponent> ) } }
How does this work?
I saw some blog where some people do this:
export default class SomeComponent extends React.Component { render() { return ( <View style={{flex:1}}> {/* code added here - start */} {React.Children.map(this.props.children, c => React.cloneElement(c, { route: this.props.route }))} {/* code added here - end */} </View> ) } }
But this does not work for me.
I am getting the following error:
Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components)
Any help would be appreciated. Thanks
To wrap one component into another with React, we add the children prop into the parent component. We create the Wrapper component that accepts the children prop. And we put children in a div. Next, we create the Child component that takes the name prop and renders some text.
Just call an alert method in the childToParent function and pass that function as a prop to the child component. And in the child component, accept the childToParent function as a prop. Then assign it to an onClick event on a button. That's it!
You can use this snippet
export default class SomeComponent extends React.Component { constructor(props) { super(props) } render() { return ( <View style={{flex:1}}> {this.props.children} </View> ) } }
You can do it like this
export default class SomeComponent extends React.Component { render() { return ( <View> {this.props.children} </View> ) } }
YupeComponent.js
import 'SomeComponent' from 'app/component/Somecomponent' export default class YupeComponent extends React.Component { render() { return ( <View style={{flex:1}}> <SomeComponent /> </View> ) } }
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