csiscs
csiscs

Reputation: 69

Cannot set state on react component

For some reason, the onSubmit() function I cannot set the state of isLoading even though I'm pretty sure I have done the same thing in the past.

    import * as React from 'react';
    import * as Redux from 'redux';
    const { connect } = require('react-redux');
    import { push } from "react-router-redux";
    import { Col, Jumbotron, Row, Well, Label, Button, FormGroup, FormControl } from 'react-bootstrap';
    import { ISession, IApplicationState } from "store";
    import './styles.scss';
    import { FeedComponent, Feed, StorageApiContext } from "api"
    import { Content, ContentLoadingWrapper } from "components";


    interface IProfileUserPageProps {
        session: ISession;
        feed: Feed;
        feedComponent: FeedComponent;
    }

    interface IProfileUserPageState {
        isLoading: boolean;
    }


    @connect(
        (state: IApplicationState) => ({
            session: state.session.data,
        }),
        (dispatch: Redux.Dispatch<any>) => ({
            navigateToLogin: () => dispatch(push("/")),
        })
    )

    export class ProfileUserPage extends React.Component<IProfileUserPageProps, IProfileUserPageState> {

        constructor() {
            super();
            this.state = { isLoading: false };
        }

        componentWillMount() {
            const {
                session,
            } = this.props;

        }

        onSubmit() {
            this.setState = ({ isLoading: true });
            var inputValue = (document.getElementById("competitors-area") as HTMLInputElement).value;
            const addTo: string[] = [];
            inputValue.split("\n").forEach(company => {
                addTo.push(company)
            });
            const context = new StorageApiContext();
            this.props.session.user.Competitors = addTo;
            context.Users.modify(this.props.session.user);
        }

The error I receive is:

ERROR in [at-loader] ./src/app/pages/profileUser/ProfileUserPage.tsx:38:28
    TS2322: Type '{ isLoaded: boolean; }' is not assignable to type '{ <K extends "isLoaded">(f: (prevState: IProfileUserPageState, props: IProfileUserPageProps) => P...'.
  Object literal may only specify known properties, and 'isLoaded' does not exist in type '{ <K extends "isLoaded">(f: (prevState: IProfileUserPageState, props: IProfileUserPageProps) => P...'.

Upvotes: 0

Views: 526

Answers (1)

Kristupas Repečka
Kristupas Repečka

Reputation: 739

setState() is not an object. It is function to be called like that:

this.setState({ isLoading: true });

Upvotes: 2

Related Questions