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"
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> ); } } }
thank you in advance for your help !
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>
)
}
You could try the following to destructure children
from this.props
:
export default class WallPaper extends Component {
render() {
const { children } = this.props;
return (
<ImageBackground
source={backgroundimg}
imageStyle={{ opacity: 0.9 }}
style={styles.container}
>
{children}
</ImageBackground>
);
}
}
It looks like your project may require propTypes for this component. Try the following to add a children
prop type. Note, you will need to install package prop-types :
// ...
import PropTypes from 'prop-types';
class WallPaper extends Component {
render() {
const { children } = this.props;
return (
<ImageBackground
source={backgroundimg}
imageStyle={{ opacity: 0.9 }}
style={styles.container}
>
{children}
</ImageBackground>
);
}
}
WallPaper.propTypes = {
children: PropTypes.node // or PropTypes.node.isRequired to make it required
};
export default WallPaper;
Hopefully that helps!
This is due to linting rule .
You can turn off rule if you don't want destructing.
If you want than you can do it like this.
export default class WallPaper extends Component {
render() {
const {children} = this.props
return (
<ImageBackground
source={backgroundimg}
imageStyle={{ opacity: 0.9 }}
style={styles.container}
>
{this.props.children}
</ImageBackground>
);
}
}
export default class WallPaper extends Component {
render() {
(this.props) => {
return (
<ImageBackground
source={backgroundimg}
imageStyle={{ opacity: 0.9 }}
style={styles.container}
>
{props.children}
</ImageBackground>
);
}
}
}
and for the class component:
export class Welcome extends Component {
render(){
const {name, hero, children} = this.props
return (
<div>
<h1> Class Component with {name} as {hero} </h1>
{children}
</div>
)
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.