2017-08-15 80 views
0

由于某些原因,即使我很确定我在过去做过同样的事情,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...'. 

回答

2

setState()不是一个对象。这是所谓的功能:

this.setState({ isLoading: true }); 
+0

哇,这是显而易见的,谢谢 – csiscs