Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

es6 react getting props in child component

I've been migrating one of my apps to ES6 on node/react and I have a question about how props are passed down to children. I read a bunch of posts and some address this while others don't. Basically, what I've seen so far is this:

export default class SomeComponent extends React.Component {
    constructor(props) {
        super(props);
    }
    render() {
        return (
            <div>
               {this.props.text} <<< Props used here
            </div>
        );
    }
}

but I've been able to get my component to work with the following:

export default class SomeComponent extends React.Component {
    constructor() {
        super(); <<< notice no props in parentheses 
    }
    render() {
        return (
            <div>
               {this.props.text} <<< Props used here
            </div>
        );
    }
}

is there a reason why I should pass the props in the parentheses for my constructor and the super call? or can I leave my code the way it is

like image 324
duxfox-- Avatar asked Apr 26 '16 15:04

duxfox--


1 Answers

You don't need to pass props to super unless you want to use this.props in constructor.

https://stackoverflow.com/a/34995257/3238350

like image 105
zarcode Avatar answered Sep 27 '22 21:09

zarcode