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.

Warn If a Component Doesn't Extend React.Component

See original GitHub issue

To support arrow functions and plain functions as “components” we need to know if we can call new on them. Examples that won’t work:

function PlainFunctionComponent(props) {
  return null;
}

var ArrowFunctionComponent = (props) => <div />;

class ClassComponent {
  render() {
    return <div><PlainFunctionComponent /><ArrowFunctionComponent /></div>;
  }
}

We need to detect if a component is a “function” or a “class” before calling new on it.

We can call new on everything if they’re plain functions as long as they return a ReactElement. However, that won’t work for null/false/string return values and we want to support those too. We also can’t call new on arrow functions. Likewise, we can’t NOT call new on classes.

Unfortunately ECMAScript doesn’t have a way to detect if something is newable or not.

The current plan is to add our own static flag to React.Component so that we can detect if something is a class.

Any class pattern that doesn’t require new should still be possible:

function ModuleComponent(props) {
  return {
    render() {
      return <div />;
    }
  };
}

I’m not sure how we can distinguish this pattern from one that requires new for warnings though. 😦

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
sebmarkbagecommented, Aug 11, 2015

@rads That might be enough. A bit fragile. It also means that classes in ECMAScript can never have a default render function. 😃

We might have more than one way of rendering in the future. E.g. prerender / postrender / renderAtIndex. That might still work though.

Will need to check if there’s any difference in terms of engine optimizations between these options.

1reaction
gaearoncommented, Aug 11, 2015

having to extend ReactComponent is a bit more annoying for stateless components

But if component is stateless you can just turn it into a function?

Read more comments on GitHub >

github_iconTop Results From Across the Web

I extended but still "Warning: React component classes must ...
Page loads as it should, but I'm getting a "React component classes must extend React.Component" warning even though I thought I extended my ......
Read more >
React.Component
React doesn't call UNSAFE_componentWillReceiveProps() with initial props during mounting. It only calls this method if some of component's props may update.
Read more >
Conditional Rendering - React
You can use variables to store elements. This can help you conditionally render a part of the component while the rest of the...
Read more >
Tutorial: Intro to React
Each React component is encapsulated and can operate independently; this allows you to build complex UIs from simple components. Inspecting the Starter Code....
Read more >
React Top-Level API
const MyComponent = React.memo(function MyComponent(props) { /* render using props */ });. React.memo is a higher order component. If your component renders the ......
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