question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Display a warning if Component is called without props

See original GitHub issue

Do you want to request a feature or report a bug?

This is a feature (enhancement).

What is the current behavior?

props parameter isn’t validated in Component and PureComponent. Omitted props is a common mistake that results in undefined this.props in constructor. This may result in a problem:

class MyComponent extends Component {
  constructor() {
    super();
    this.state = { a: 1, b: this.props.b }; // cannot read b property of undefined
  }
}

A problem may be harder to determine if previously working code stops working when refactored:

class MyComponent extends Component {
  constructor() {
    super(); // no error
  }

  componentWillMount() {
    // rewriting this to constructor code will result in situation above
    this.setState({ a: 1, b: this.props.b });
  }
}

What is the expected behavior?

Component and PureComponent validate props parameter to be an object, or arguments > 0 at least and display a warning in development mode in case props isn’t passed from child constructor.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

React 16.7.0-alpha.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
sophiebitscommented, Oct 31, 2018

This is a good idea, but since we made super(); work if you don’t access this.props in the constructor, and using this.props in the constructor is relatively rare, I don’t think it is worth it to add this at super(); time.

However we could perhaps have a warning when accessing this.props if we make it a getter?

1reaction
sagar-gavhanecommented, Nov 1, 2018

Hi @bisubus, I agree with you but how can it helps to developers. You can also try static state or methods for the same.

Check out this article for more reference. https://michalzalecki.com/react-components-and-class-properties/

import React, { Component } from "react";

class App extends Component {
  state = {
    appName: this.props.appName
  };

  render() {
    return (
      <React.Fragment>
        <div className="container my-5">
          <h2>{this.state.appName}</h2>
        </div>
      </React.Fragment>
    );
  }
}

export default App;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting error message "li: 'key' is not a prop" - reactjs
Warning : li: 'key' is not a prop. Trying to access it will result in 'undefined' being returned. If you need to access...
Read more >
How passing props to component works in React
Master how to pass props (properties) to components in React with this useful beginner's guide, including demo code.
Read more >
Reacting to Prop Changes in a React Component
When building a component using React there is often a requirement to create a side effect when one of the component props changes....
Read more >
Unknown Prop Warning
The unknown-prop warning will fire if you attempt to render a DOM element with a prop that is not recognized by React as...
Read more >
Displaying Data with Props - From JavaScript to React
In the same way, you can pass pieces of information as properties to React components. These are called props . Similar to a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found