0

Here is a strange problem, here is the code:

...
const PrivateRoute = ({status, component: Component, ...rest}) => (
    <Route {...rest} render={ props => (
      status ?
        (<Component {...props}/>) :
        (<Redirect to="/login"/>)
    )}/>
)

function mapStateToProps(state) {
  return {
    status: state.user.status
}

export default connect(mapStateToProps)(PrivateRoute)

The error when doing webpack -d -w:

ERROR in ./public/javascripts/src/admin/components/PrivateRoute.jsx
Module build failed: SyntaxError: Unexpected token (4:53)

  2 | import { Route, Redirect } from 'react-router-dom'
  3 | 
> 4 | const PrivateRoute = ({status, component: Component, ...rest}) => (
    |                                                      ^
  5 |   <Route {...rest} render={ props => (
  6 |     status ?
  7 |       (<Component {...props}/>) :

The code is just follow the tutorial over here. However, the code blow also uses the '...' in Route {...rest}. When I remove the first '...', the second and third doesn't produce the error. Why does that happen?

2 Answers 2

2

This is experimental syntax. You need to include object-rest-spread plugin in your babel config. Or use a preset that includes this plugin. For example stage-3

As of this

When I remove the first '...', the second and third doesn't produce the error. Why does that happen?

<Route {...rest} is handled by jsx plugin included into react preset. Repl.

Sign up to request clarification or add additional context in comments.

1 Comment

No problem. Glad I could help :)
0

Update you .eslintrc.js, set experimentalObjectRestSpread to ecmaFeatures as true.

parserOptions: {
  ecmaVersion: 7,
  sourceType: 'module',
  ecmaFeatures: {
    jsx: true,
    experimentalObjectRestSpread: true
  }
}

See https://eslint.org/docs/rules/rest-spread-spacing

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.