Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Native: How to wrap Child components under another class component

Tags:

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

like image 918
user5712342 Avatar asked Feb 22 '16 06:02

user5712342


People also ask

How do you wrap the child component in React?

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.

How do you pass props from one child component to another?

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!


2 Answers

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>     )   } } 
like image 150
Muhammmad Hadi Rajani Avatar answered Oct 13 '22 17:10

Muhammmad Hadi Rajani


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>     )   } } 
like image 23
angry kiwi Avatar answered Oct 13 '22 18:10

angry kiwi