Reactjs React组件:声明或语句应为ts(1128)

Reactjs React组件:声明或语句应为ts(1128),reactjs,authentication,Reactjs,Authentication,我是个新手,经常在语法方面遇到麻烦。 以下代码给出了错误:声明或语句应为ts1128 浏览互联网,我得出的结论是,很可能是某个括号丢失了 import React, { Component } from 'react'; import AuthContext from './AuthContext'; // Then create a provider Component to update children Components once the user role changes clas

我是个新手,经常在语法方面遇到麻烦。 以下代码给出了错误:声明或语句应为ts1128

浏览互联网,我得出的结论是,很可能是某个括号丢失了

import React, { Component } from 'react';
import AuthContext from './AuthContext';

// Then create a provider Component to update children Components once the user role changes
class AuthProvider extends Component {
    constructor() {
      super()
      this.state = {
          role: "none"
      }
  }

  render() {
    return (
      <AuthContext.Provider value={{
        state: this.state,
        isAuthenticated: () => {
          if (this.state.role == "kursleiter" || this.state.role == "admin") {
            return true
          }
          return false},
        setRole: (newRole) => this.setState({
            role: newRole
        })
      }}>
        {this.props.children}
      </AuthContext.Provider>
    )
  }
}

export default AuthProvider

您需要将代码提取到函数中,以便可以调用它们。例如:

const isAuthenticated = () => {
 // ...
}
现在,您可以使用:

value={{
  state: this.state,
  isAuthenticated: isAuthenticated()

之前,您使用的是函数,但不是它的调用。因此,react为预期的语句抛出了错误。

您需要将代码提取到函数中,以便可以调用它们。例如:

const isAuthenticated = () => {
 // ...
}
现在,您可以使用:

value={{
  state: this.state,
  isAuthenticated: isAuthenticated()

之前,您使用的是函数,但不是它的调用。因此,react向您抛出了预期语句的错误。

在我的网站上超级愚蠢:文件名不匹配,仅此而已。

在我的网站上超级愚蠢:文件名不匹配,仅此而已。

谢谢,但这并不能解决我的问题。谢谢,但这并不能解决我的问题。这似乎很好,还有其他线索吗?这似乎很好,还有其他线索吗?