好的,我有一个问题,我一直在关注 Udacity 类(class)。 问题是整个类(class)应用程序都包含在一个文件中,并且越来越难以仔细研究以找到问题所在。
某个时候出现了一个错误,我认为这是一个语法错误,我在其中添加或遗漏了一个括号或类似的内容,但我找不到错误发生的位置。
我认为它特定于下面的 App
组件,但看不出我做错了什么。
VS Code 中的语法突出显示指向以下行,其中 const
未像在代码的其他区域中那样突出显示。
const Context = React.createContext()
class App extends React.Component {
const Context = React.createContext()
class ConnectedApp extends React.Component {
render() {
return (
<Context.Consumer>
{(store) => (
<App store={store}/>
)}
</Context.Consumer>
)
}
}
class Provider extends React.Component {
render() {
<Context.Provider value={this.props.store}>
{ this.props.children }
</Context.Provider>
}
}
componentDidMount () {
const { store } = this.props
store.dispatch(handleIitialData())
store.subscribe( () => this.forceUpdate())
}
render() {
const { store } = this.props
const { todos, goals, loading } = store.getState()
if(loading) { return <h3>Loading</h3> }
return(
<div>
< Todos todos={todos} store={this.props.store} />
< Goals goals={goals} store={this.props.store} />
</div>
)
}
}
错误
babel.min.js:27 Uncaught SyntaxError: Inline Babel script: Unexpected token (108:18)
106 | class App extends React.Component {
107 |
> 108 | const Context = React.createContext()
| ^
109 |
110 | class ConnectedApp extends React.Component {
111 | render() {
at r.l.raise (babel.min.js:27)
at r.c.unexpected (babel.min.js:27)
at r.c.expect (babel.min.js:27)
at r.m.parseMethod (babel.min.js:27)
at r.parseClassMethod (babel.min.js:28)
at r.m.parseClassBody (babel.min.js:27)
at r.m.parseClass (babel.min.js:27)
at r.m.parseStatement (babel.min.js:27)
at r.parseStatement (babel.min.js:27)
at r.m.parseBlockBody (babel.min.js:27)
最佳答案
这是因为您尝试在该类中使用的语法不在您的 babel 配置中或不受您的引擎(节点/浏览器)支持(这只是 class public fields proposal 的提议)。
您应该在您的 babel ( stage-3) 中添加对该语法的支持,知道该语法存在无法作为该语言的最终特性通过的风险。
或者在类外声明 Context 常量(或在类的构造函数内,然后使用 this 上下文访问它)。
示例(来自 official documentation 但适合您的代码):
// the Context is created outside App
const Context = React.createContext();
// Also ConnectedApp and Provider are OUTSIDE App (The console doesnt complain now, but it will after you fix the one with const)
class Provider extends React.Component {
render() {
<Context.Provider value={this.props.store}>
{ this.props.children }
</Context.Provider>
}
}
class App extends React.Component {
componentDidMount () {
const { store } = this.props
store.dispatch(handleIitialData())
store.subscribe( () => this.forceUpdate())
}
render() {
const { store } = this.props
const { todos, goals, loading } = store.getState()
if(loading) { return <h3>Loading</h3> }
return(
<div>
< Todos todos={todos} store={this.props.store} />
< Goals goals={goals} store={this.props.store} />
</div>
)
}
}
// ConnectedApp goes after App since it uses App internally
class ConnectedApp extends React.Component {
render() {
return (
<Context.Consumer>
{(store) => (
<App store={store}/>
)}
</Context.Consumer>
)
}
}
您的代码中还有其他 React 概念错误,但由于您正在学习一门类(class),我猜这是目前故意的。
关于javascript - React 应用程序中的意外 token 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50970379/