ElJefeJames ElJefeJames - 1 year ago 113
React JSX Question

Why can't I access default props in react constructor, but I can in render?

Per the title, why does the initial this.props fail? And more practically, how do you work around this in cases where you rely on the props within your constructor? For instance I would like to reference props within my subscriptions?

export default class AboutBox extends TrackerReact(React.Component) {
static defaultProps = {
title: 'Undefined Product',
constructor() {
console.log(this.props.title); //this fails
render() {
console.log(this.props.title); //this works

Answer Source

You have to put the props on the constructor args, and pass them to super


use props inside constructor, this works also, but have some problem in transpilation against IE

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download