Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How i can destructuring {this.props.children}?

I want to add a background to my mobile app but when i use "this.props.children" eslint say me "Must use destructuring props assignment". Why i can destructuring this props ?

This my code ,

export default class WallPaper extends Component {
  render() {
    return (
      <ImageBackground
        source={backgroundimg}
        imageStyle={{ opacity: 0.9 }}
        style={styles.container}
      >
        {this.props.children}
      </ImageBackground>
    );
  }
}

when i use this code

export default class WallPaper extends Component {
  render() {
    const { children } = this.props;
    return (
      <ImageBackground
        source={backgroundimg}
        imageStyle={{ opacity: 0.9 }}
        style={styles.container}
      >
        {children}
      </ImageBackground>
    );
  }
}

i have this error "'children' is missing in props validation" error

when i use this code ,

export default class WallPaper extends Component {
  render() {
     (this.props) => {
    return (
      <ImageBackground
        source={backgroundimg}
        imageStyle={{ opacity: 0.9 }}
        style={styles.container}
      >
        {props.children}
      </ImageBackground>
    );
    }
  }
}

i have this error, enter image description here

thank you in advance for your help !

like image 715
Michel Melhem Avatar asked Dec 27 '18 16:12

Michel Melhem


People also ask

Can I Destructure props in React?

In destructuring, It does not change an array or any object, it makes a copy of the desired object or array element by assigning them in its own new variables, later we can use this new variable in React (class or functional) components. It makes the code more clear.

How do you pass the props kids in React?

Passing Props to Children in React Using the Context API Context allows you to pass props down to an entire tree of components. It is extremely useful because you have an App component at the top but want to pass down an event handler to a child component at the bottom of the tree.

What does props with children mean in React?

Props is simply an abbreviation for properties. In React, we utilize props to send data from one component to another (from a parent component to a child component or multiple children components). They come in handy when you want the data flow in an app to be dynamic.

How do you Destructure state in React?

Destructuring state Destructuring states is similar to props. Here is syntax to Destructuring states in React: import React, { Component } from 'react' class StateDemp extends Component { render() { const {state1, state2, state3} = this.


1 Answers

The answers are missing for functional component cases. children is just a prop passed to the component. In order to use it like you are using props.url you need to add it to that list so it can be "pulled out" of the props object.

export const Welcome = ({name, hero, children}) => {
        return (
        <div>
           <h1> Class Component with {name} as {hero} </h1>
            {children}
        </div>
        )
    }
like image 113
Rajon Kobir Avatar answered Oct 16 '22 02:10

Rajon Kobir