0
votes

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.

3
You seem to have forgotten the function keyword - Bergi

3 Answers

1
votes

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
  }
}
0
votes

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.

0
votes

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) {...}