[英]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.