Skip to content

Cannot be used new contextType API in constructor? #13944

Closed

Description

@maisumakun

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

What is the current behavior? Using Component.contextType, this.context keeps null in class component's constructor.

What is the expected behavior?

  • this.context can be obtained in constructor
  • if implementing of the feature is inappropriate, write in documentations that this.context cannot be used in constructor.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React? React 16.6 (older React doesn't have Component.contextType) in Chrome 70 (Windows 7)

Activity

  1. sebmarkbage commented on Oct 24, 2018

    @sebmarkbage
    Contributor

    Hm. Yea. This is a bug I think. It should probably be allowed.

  2. gaearon commented on Oct 24, 2018

    @gaearon
    Collaborator

    Want to send a failing unit test and/or fix?

  3. aweary commented on Oct 24, 2018

    @aweary
    Contributor

    @maisumakun how are you calling super in the constructor? If you pass context to super it's accessible as expected.

    https://codesandbox.io/s/l47jw5ryyz

    class App extends React.Component {
      static contextType = Context
      constructor(props, context) {
        super(props, context)
        // this.context now works
      }

    This seems consistent with this.props. If you don't pass props to super you cannot access this.props in the constructor. Should this.context work differently?

  4. maisumakun commented on Oct 25, 2018

    @maisumakun
    Author

    @aweary Oh, thanks!

    constructor(props, context) reminded me a code for legacy Context API, but necessary for contextType too. (this should be documented anywhere)

  5. gaearon commented on Nov 1, 2018

    @gaearon
    Collaborator

    Please feel free to send a documentation PR to section about using contextType.
    https://github.com/reactjs/rfcs

  6. OdenigboGodfrey commented on Mar 4, 2021

    @OdenigboGodfrey

    super(props, context) is deprecated now also :sigh

  7. molimat commented on Jul 12, 2021

    @molimat

    but actually almost the whole class component concept is deprecated. lol

  8. chbdetta commented on Jul 21, 2021

    @chbdetta

    but actually almost the whole class component concept is deprecated. lol

    Simply untrue. There is no plan deprecating class component.

  9. chbdetta commented on Jul 13, 2022

    @chbdetta

    According to:
    https://github.dev/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts#L77-L79

    component class with a constructor signature constructor(props: MyProps, context: MyContext) is no longer valid JSXElementConstructor . So this usage is now prohibited by TS

    Any workaround?

  10. joshuakb2 commented on Jun 26, 2023

    @joshuakb2

    You can access this.context from UNSAFE_componentWillMount, but you'll get warnings in strict mode.

    This issue was closed previously because super(props, context) was still available, but now it's deprecated. What's the way forward?

  11. kubijo commented on Sep 30, 2023

    @kubijo

    You can access this.context from UNSAFE_componentWillMount, but you'll get warnings in strict mode.

    This issue was closed previously because super(props, context) was still available, but now it's deprecated. What's the way forward?

    Bumping this with the same reason ... if there is no new way of achieving this, this should not be deprecated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions