如何解决此错误:TypeError:this.state不是函数

时间:2020-11-05 18:53:50

标签: javascript reactjs

我无法解决此问题,该如何解决?

import React, { Component } from "react";
import "./Main.css";
import { FaSearch, FaEdit, FaWindowClose } from "react-icons/fa";

export default class Main extends Component {
  state = {
    pesquisaNFE: "",
    resultado: [],
  };

  handleSubmit = (e) => {
    e.preventDefault();
    const { resultado } = this.state;
    let { pesquisaNFE } = this.state;
    pesquisaNFE = pesquisaNFE.trim();

    if (resultado.indexOf(pesquisaNFE) !== -1) return;

    const newSearch = [...resultado];

    this.state({
      resultado: [...newSearch, pesquisaNFE],
    });
  };

错误:

enter image description here

1 个答案:

答案 0 :(得分:4)

要更新状态,您需要致电this.setState

this.state({更改为this.setState({