【问题标题】:Error in declaring a variable inside the component class and outside the render function to use it inside the render function in react在组件类内部和渲染函数外部声明变量以在渲染函数中使用它时出错
【发布时间】:2019-03-04 16:25:56
【问题描述】:

在下面的代码中,当我通过解构声明可变字符时

const { characters } = this.state;

我收到一个意外的令牌错误。

import React, { Component } from "react";
import Table from "./Table";
class App extends Component {
  state = {
    characters: [
      {
        name: "Charlie",
        job: "Janitor"
      },
      {
        name: "Mac",
        job: "Bouncer"
      },
      {
        name: "Dee",
        job: "Aspring actress"
      },
      {
        name: "Dennis",
        job: "Bartender"
      }
    ]
  };
  removeCharacter = index => {
    const { characters } = this.state;

    this.setState({
      characters: characters.filter((character, i) => {
        return i !== index;
      })
    });
  };
   const { characters } = this.state;
  render() {
   
    return (
      <React.Fragment>
        <div className="App">
          <h1>Hello, React!</h1>
        </div>

        <div className="container">
          <Table
            characterData={characters}
            removeCharacter={this.removeCharacter}
          />
        </div>
      </React.Fragment>
    );
  }
}

export default App;

这里是一个沙箱代码:sandbox with the error, look at the app.js file

当我将声明放入渲染函数时没有问题, 查看correct 代码沙箱。

另外,当我在(App.js 文件的)渲染函数之外创建一个变量时,没有 const 或 let 例如:

x=1;

当我在渲染函数中将它用作 this.x 时,它工作正常,但是当我用 let、const 或 var 声明它时,它会抛出一个 Unexpected token 错误。

你如何解释这种行为?

【问题讨论】:

    标签: reactjs class variables render


    【解决方案1】:

    您在渲染方法之外破坏了字符。

    import React, { Component } from "react";
    import Table from "./Table";
    class App extends Component {
      state = {
        characters: [
          {
            name: "Charlie",
            job: "Janitor"
          },
          {
            name: "Mac",
            job: "Bouncer"
          },
          {
            name: "Dee",
            job: "Aspring actress"
          },
          {
            name: "Dennis",
            job: "Bartender"
          }
        ]
      };
      removeCharacter = index => {
        const { characters } = this.state;
    
        this.setState({
          characters: characters.filter((character, i) => {
            return i !== index;
          })
        });
      };
      render() {
        const { characters } = this.state;
        return (
          <React.Fragment>
            <div className="App">
              <h1>Hello, React!</h1>
            </div>
    
            <div className="container">
              <Table
                characterData={characters}
                removeCharacter={this.removeCharacter}
              />
            </div>
          </React.Fragment>
        );
      }
    }
    
    export default App;
    

    【讨论】:

    • 为什么这不合法?
    • 你不能在班级级别使用破坏{ }
    • 啊,好的,谢谢!但是为什么我不能用 const,let 或 var 声明变量,但是当我只使用变量名来声明变量时,例如 x = 1;已验证?上面的声明是类级别的
    猜你喜欢
    • 2019-09-28
    • 2019-11-22
    • 2020-06-20
    • 1970-01-01
    • 2020-10-20
    • 1970-01-01
    • 2021-05-28
    • 2021-08-08
    • 1970-01-01
    相关资源
    最近更新 更多