简体   繁体   中英

React: is not defined no-undef

When I refresh the error message in clearAll() is:

./src/App.js

Line 22: 'notes' is not defined no-undef

Line 23: 'notes' is not defined no-undef

How do I define the the properties to use>

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 = [];
}

Here you are trying to update your state variable. For that you can use setState function.
https://reactjs.org/docs/react-component.html#setstate
http://www.cluemediator.com/state-react-js/

You clearAll function should be look like below.

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

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

Hope this will work for you!

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

const { note, notes } = this.state;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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