Skip to content

Remove readonly keyword from the state #600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

alamenai
Copy link
Contributor

@alamenai alamenai commented Jan 6, 2023

Based on this PR we don't have to put readonly for our states, because React.Component<P,S> already marks them as immutable.

Based on this [PR](DefinitelyTyped/DefinitelyTyped#26813) we don't have to put `readonly` for our states, because `React.Component<P,S>` already marks them as immutable.
Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to me, it's still beneficial to add readonly:

class Provider extends React.Component<
  { children?: React.ReactNode },
  ProviderState
> {
  public state = {
    themeColor: "red",
  }

  render() {
    // should TS error but doesn't :(
    this.state = {themeColor: 'blue'}
    return null
  }
}

Playground Link

@filiptammergard
Copy link
Collaborator

Closing, since the context page has been reworked and does no longer include a class-based version. Feel free to comment on the new context page!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants