omarab
omarab

Reputation: 9

Uncaught SyntaxError: Unexpected token export (Redux)

I'm trying to create a reducer in react with the code below, but I keep getting this error in the console:

Uncaught SyntaxError: Unexpected token export

const INIT_STATE = [];

export default (state = INIT_STATE, action) {

  switch(action.type) {
    default: state
  }
}

I'm still trying to figure my way around redux and don't know exactly how to solve this error.

Upvotes: 0

Views: 3257

Answers (3)

user6826724
user6826724

Reputation:

This is improper syntax. You need to add the function keyword before the function signature, or define it as an arrow function like Sam suggested.

i.e. export default function (state = INIT_STATE, action) {...} or export default (state = INIT_STATE, action) => {...}, instead of export default (state = INIT_STATE, action) {...}

Upvotes: 0

markerikson
markerikson

Reputation: 67439

My guess is that you're trying to run that code without compiling it first. The ES6 import/export syntax is still not natively supported in most environments - ES6 modules have to be compiled to another format first, usually using Babel.

Upvotes: 0

Revln9
Revln9

Reputation: 847

This happens when you have multiple exports on the same file and you add a default export to one of them , so the solution is either to export one module by using export default or just export if you want to export multiple objects , functions...etc in the same file

Another thing to mention is the way you call a function , es6 introduces the arrow function

instead of this (arg1 , arg2 ){ .... } you should do this (arg1 , arg2 ) => {.....}

so for your case

   const INIT_STATE = [];

export (state = INIT_STATE, action) => {

switch(action.type) {
    default: state
  }
}

Upvotes: 1

Related Questions