繁体   English   中英

反应:未定义 no-undef

[英]React: is not defined no-undef

当我刷新 clearAll() 中的错误消息时:

./src/App.js

第 22 行:'notes' 未定义 no-undef

第 23 行:'notes' 未定义 no-undef

如何定义要使用的属性>

import React, { Component } from 'react';
import Route from 'react-router-dom';
import './App.css';
import Header from './components/Header';
import Router from './components/Router';
import Home from '.Home';
import AddNote from '/AddNote';
import EditNote from '/EditNote';


class App extends React.Component {

constructor(props) {
    super(props);
    this.state = {
        note: "",
        notes: []
    };
}

clearAll() {
    notes.length = 0;
    notes = [];
}

在这里,您尝试更新state变量。 为此,您可以使用setState函数。
https://reactjs.org/docs/react-component.html#setstate
http://www.cluemediator.com/state-react-js/

您的clearAll函数应如下所示。

constructor(props) {
    super(props);
    this.state = {
        note: "",
        notes: []
    };
    this.clearAll = this.clearAll.bind(this);
}

clearAll() {
   this.setState({notes: []});
}

希望这对您有用!

notenotes保存在组件的 state 中,您可以分别通过this.state.notethis.state.notes在组件中访问它们。

const { note, notes } = this.state;

暂无
暂无

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

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