简体   繁体   English

React.js中setState函数“ this”的范围问题

[英]The Scope Problem of 'this' of setState function in Reactjs

Actually I'm using umi instead of react. 实际上,我使用的是umi而不是react。 And I found an error of the scope problem of 'this.setState' in function handleUsername. 而且我在handleUsername函数中发现“ this.setState”的范围问题的错误。

I have tried to replace 'this.setState' with 'setState' and it shows can not found function setState. 我试图用“ setState”替换“ this.setState”,它显示找不到函数setState。

here is my code: 这是我的代码:

import React from 'react';
import styles from './index.css';
// import router from 'umi/router';
import {Input, Button} from 'antd';
import { connect } from 'dva';
import { Dispatch } from 'react';

// const namespace = 'global';

interface DisType{
  dispatch: Dispatch<{}>;
}

export default connect ()(( props:DisType ) => {
  const handleClick = () =>{
    // router.push('/homepage/');
    //test dva
    props.dispatch({
      type: 'global/setUserInfo',
      payload: {        
        username: '1111',
        password: '222',
      }
    });
    props.dispatch({
      type:'global/login',
    });

  }
  const handleUsername = (event: any) => {
    this.setState({
      username: event.target.value,
    })
  }
  return (
    <div className={styles.normal}>
      <div className={styles.welcome} />
      <h1 className={styles.title}>login</h1>
      <ul className={styles.list}>
        <li><label>username:</label><Input className={styles.input} onChange={handleUsername} placeholder="username"/></li>
        <li><label>password:</label><Input className={styles.input} placeholder="password"/></li>
        <li>
          <Button type="primary" shape="round" onClick={handleClick}>login</Button>
        </li>
      </ul>
    </div>
  );
});

I want to change state in this component. 我想更改此组件中的状态。 Please help me. 请帮我。

here is my new code below: 这是我的新代码如下:

import React from 'react';
import styles from './index.css';
// import router from 'umi/router';
import {Input, Button} from 'antd';
import { connect } from 'dva';

class Index extends React.Component {
  handleClick = () =>{
    this.props.dispatch({
      type: 'global/setUserInfo',
      payload: {        
        username: '1111',
        password: '222',
      }
    });
    this.props.dispatch({
      type:'global/login',
    });
  }
  handleUsername = (event: any) => {
    this.setState({
      username: event.target.value,
    })
  }
  public render() {
    return (
      <div className={styles.normal}>
        <div className={styles.welcome} />
        <h1>智能教室系统</h1>
        <h1 className={styles.title}>login</h1>
        <ul className={styles.list}>
          <li><label>username:</label><Input className={styles.input} onChange={this.handleUsername} placeholder="username"/></li>
          <li><label>password:</label><Input className={styles.input} placeholder="password"/></li>
          <li>
            <Button type="primary" shape="round" onClick={this.handleClick}>login</Button>
          </li>
        </ul>
      </div>
    );
  }
}
function mapStateToProps(state:any) {
  return {
    username: state.global.username,
    password: state.global.password,
  };
}
export default connect (mapStateToProps)(Index);

It shows dispatch does not exist. 它表明调度不存在。 And I can not use this.props.username to get username from state. 而且我不能使用this.props.username从状态获取用户名。

You appear to have written a Function Component, but they don't typically have states (so you can't set their states). 您似乎已经编写了一个功能组件,但是它们通常没有状态(因此无法设置其状态)。 Write a Class Component instead. 改为编写类组件。

See the React documentation for the syntax differences . 有关语法差异,请参见React文档

If you are using a very recent version of React, then you could use a Function Component with a State Hook instead. 如果您使用的是React的最新版本,则可以使用带有状态挂钩的功能组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM