无法在反应组件上设置状态

时间:2017-08-15 16:22:19

标签: reactjs typescript load state

出于某种原因,onSubmit()函数我无法设置isLoading的状态,即使我很确定我过去做过同样的事情。

    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);
        }

我收到的错误是:

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...'.

1 个答案:

答案 0 :(得分:2)

setState()不是对象。它的功能就像那样:

this.setState({ isLoading: true });