Before rolling back (as suggested by @torvin’s answer), please read through https://github.com/DefinitelyTyped/DefinitelyTyped/pull/26813#issuecomment-400795486.
This was not meant to be a regression – the solution is to use state
as a property. It is better than previous approach (setting state
in a constructor) because:
- you don’t need constructor at all anymore
- you can’t forget to initialize state (it is now a compile-time error)
For example:
type Props {}
type State {
active: boolean
}
export default class Square extends React.Component<Props, State> {
public readonly state: State = {
active: false
}
public render() {
//...
}
}
Another approach:
type Props {}
const InitialState = {
active: false
}
type State = typeof InitialState
export default class Square extends React.Component<Props, State> {
public readonly state = InitialState
public render() {
//...
}
}