[英]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: []});
}
希望这对您有用!
note
和notes
保存在组件的 state 中,您可以分别通过this.state.note
和this.state.notes
在组件中访问它们。
const { note, notes } = this.state;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.