Decorator feature not working (unexpected token)

六眼飞鱼酱① 提交于 2021-01-28 12:57:30

问题


Just tried to use decorators in React:

import React from 'react';
import Fade from './Transitions/Fade'
import withVisible from './withVisible'

@withVisible()
const App = props =>
    <Fade visible={props.visible} duration={500}>
        Hello
    </Fade>

export default App

If I use the common way ( withVisible()(App) ) then it's working properly. (My guess is that NodeJS can't compile my code with the @ ) [Syntax error: Unexpected token (@) ]

import React from 'react'

const withVisible = () => Component =>
    class WithVisible extends React.Component {
        state = {
            visible: true
        }
        render() {
            return (
                <Component visible={this.state.visible} {...this.props}/>
            )
        }
    }

export default withVisible

回答1:


Probably your .babelrc do not have added decorator plugin. Try this: https://babeljs.io/docs/plugins/transform-decorators




回答2:


You need transform-decorators-legacy babel plugin to make this syntax work.



来源:https://stackoverflow.com/questions/50257334/decorator-feature-not-working-unexpected-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!