0

I'm trying to create a class with TypeScript that extends React.Component, but I'm having this error:

Class 'Provider' incorrectly extends base class 'Component<{}, {}>'.
Types of property 'render' are incompatible. 
Type '(props: any) => any' is not assignable to type '() => false | Element'.

Here's the code:

import * as React from "react";

export default class Provider extends React.Component {
  props;
  getChildContext() {
    const { children, ...context } = this.props;
    return context;
  }
  render(props) {
    const { children } = props;
    return children[0];
  }
}

2 Answers 2

3

You have to specify the props when extending React.Component.

From the docs:

import * as React from "react";

export interface HelloProps { compiler: string; framework: string; }
    // 'HelloProps' describes the shape of props.
    // State is never set so we use the 'undefined' type.
export class Hello extends React.Component<HelloProps, undefined> {
        render() {
            return <h1>Hello from {this.props.compiler} and {this.props.framework}!</h1>;
        }
    }

In addition to this: render doesn't accept an argument. render() is correct.

Sign up to request clarification or add additional context in comments.

Comments

2

The error message is telling you that render() doesn't expect an argument. I suspect you are supposed to be using this.props?

Also your definition of an attribute props of type any is going to hide the type of props attribute in the base class so the compiler won't be able to work out the type of children[0].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.