首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >设置状态不设置状态

设置状态不设置状态
EN

Stack Overflow用户
提问于 2019-03-06 17:24:46
回答 3查看 47关注 0票数 0

我有一个非常简单的具有单一状态的组件,我在创建组件时初始化状态,并在提交表单时尝试更改它。

由于某种原因,它不能工作。

代码语言:javascript
复制
import React, { Component } from "react";

export default class AddTodo extends Component {
  constructor() {
    super();
    this.state = {
      submited: false
    };
  }

  handleSubmit = event => {
    this.setState = {
      submited: true
    };
    alert("Submited state: " + this.state.submited);
    event.preventDefault();
  };

  render() {
    return (
      <div className="container mt-3">
        <form onSubmit={this.handleSubmit}>
          <div className="input-group mb-3">
            <input
              type="text"
              className="form-control"
              placeholder="New Todo Description Here"
            />
            <div className="input-group-append">
              <button className="btn btn-primary" type="submit">
                Add Todo
              </button>
            </div>
          </div>
        </form>
      </div>
    );
  }
}
EN

回答 3

Stack Overflow用户

发布于 2019-03-06 17:28:26

this.setState是一个函数,所以调用它就像使用括号()一样。

代码语言:javascript
复制
this.setState({
    submited: true
});
票数 3
EN

Stack Overflow用户

发布于 2019-03-06 17:26:41

代码语言:javascript
复制
import React, { Component } from "react";

export default class AddTodo extends Component {
  constructor() {
    super();
    this.state = {
      submited: false
    };
  }

  handleSubmit = event => {
    this.setState({
      submited: true
    },()=>{
          alert("Submited state: " + this.state.submited);
          event.preventDefault();
    });

  };

  render() {
    return (
      <div className="container mt-3">
        <form onSubmit={this.handleSubmit}>
          <div className="input-group mb-3">
            <input
              type="text"
              className="form-control"
              placeholder="New Todo Description Here"
            />
            <div className="input-group-append">
              <button className="btn btn-primary" type="submit">
                Add Todo
              </button>
            </div>
          </div>
        </form>
      </div>
    );
  }
}
票数 0
EN

Stack Overflow用户

发布于 2019-03-06 17:37:51

正如您所看到的,here setState是一个函数,但是您正在为它赋值。

问题出在这里:

代码语言:javascript
复制
this.setState = {
      submited: true
};

它应该是:

代码语言:javascript
复制
this.setState({submitted: true}); // Mind the typo also

为了快速说明,React团队在版本16.8中发布了react钩子,作为对具有一些缺点的类组件的替代。我会试一试,文档是here的。

使用钩子,您的代码将如下所示:

代码语言:javascript
复制
import React, { useState } from 'react';

function AddTodo() {
  // Declare a new state variable, which we'll call "count"
  const [isSubmitted, setSubmission] = useState(false);

  function handleSubmit(event) {
     setSubmission(true);
     event.preventDefault();
  }

  return (
    <div className="container mt-3">
    <form onSubmit={handleSubmit}>
      <div className="input-group mb-3">
        <input
          type="text"
          className="form-control"
          placeholder="New Todo Description Here"
        />
        <div className="input-group-append">
          <button className="btn btn-primary" type="submit">
            Add Todo
          </button>
        </div>
      </div>
    </form>
  );
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55019573

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档