Skip to content

16.4.4 version introduced an issue with setting state in a constructor #26898

@dshuvaev

Description

@dshuvaev

After updating typings to @types/ 16.4.4. we got set of these errors:
error TS2540: Cannot assign to 'state' because it is a constant or a read-only property.
The code that was broken:

constructor(props) {
        super(props);
        this.state = {
            stateProperty: initialValue
        }
    }

The fix is to use

constructor(props) {
        super(props);
        this.setState({
            stateProperty: initialValue
        });
    }

Previous version we used was @types/ 16.0.40
ReadOnly property should be allowed to set in a constructor.

Authors: @johnnyreilly, @bbenezech, @pzavolinsky, @digiguru, @ericanderson, @morcerf, @tkrotoff, @DovydasNavickas, @onigoetz, @theruther4d, @guilhermehubner, @ferdaber, @jrakotoharisoa

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions