react: this.props is not available in Component constuctor
versions: node 0.12, react 13, babel - tested with both 4.7.4 and 5.0.8 I’m playing with new es6 syntax and figured out weird behaviour.
export default class MessageBox extends React.Component {
constructor() {
super();
console.log(this.props);
}
...
}
This code snippet will print undefined
. In fact, another weird stuff is that console.log(this)
will print an object with props
property.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 7
- Comments: 19 (2 by maintainers)
Try passing props to super call:
As for
console.log(this)
Chrome will print a “live” object to console and React assignsprops
on the constructed instance right after the construction in addition to passing them to constructor.Just use
props
instead ofthis.props
. Sincethis
doesn’t exist yet.Why this issue is closed? I am still facing the same issue. I get response for console.log(this) but console.log(this.props) is undefined 😦 Please give any work around for this it will be very helpful 👍
@sankety
You are probably missing
super(props)
call in your constructor. Add it, and everything will be fine.@nemo
One mistake I see in your example is that you’re not declaring
propTypes
anddefaultProps
asstatic
. This is wrong:This is right: