【问题标题】:ReactJS: Parsing error: Export "NextQuestion" is not definedReactJS:解析错误:未定义导出“NextQuestion”
【发布时间】:2020-10-09 05:50:22
【问题描述】:

我对为什么我的箭头函数没有导出感到困惑。我收到了错误“解析错误:未定义导出‘NextQuestion’”,我不知道为什么。这是我的代码:

import React, { Component } from "react"

NextQuestion = () => {
    var neww = true
    this.setState((state) => {
        return {continue: neww}
        }
    )
}

LostALife = () => {
    var newLives = this.state.lives - 1
    this.setState((state) => {
        return {lives: newLives}
        }
    )
}

export {NextQuestion, LostALife};

【问题讨论】:

  • 我怀疑您在这些方法中使用了this.setStatethis.state,因此如果您将这些方法包含在class 组件中,它们将按原样工作。但如果它们在类组件之外使用,则实现可能无法正常工作。

标签: reactjs function react-native react-redux helper


【解决方案1】:

你应该定义你的变量,使用 const NextQuestion,你可以在下面看到更新的代码

import React, { Component } from "react"

const NextQuestion = () => {
    var neww = true
    this.setState((state) => {
        return {continue: neww}
        }
    )
}

const LostALife = () => {
    var newLives = this.state.lives - 1
    this.setState((state) => {
        return {lives: newLives}
        }
    )
}

export {NextQuestion, LostALife};

【讨论】:

  • 哈哈哈——我不知道这个错误这么容易修复。当我再次这样做时,我也一定会记住你的答案。非常感谢人! :D
猜你喜欢
  • 2019-04-19
  • 2020-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-29
  • 1970-01-01
  • 2016-06-26
  • 2011-04-17
相关资源
最近更新 更多