Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to access JSON property with hyphen “-” in React Js(Babel)

I'm building a small React components (compile with Babel). I have to access property with a dash inside the name.

this.setState({
  newArtist: {
    birthdate: artist.life-span.begin
  }
});

This code throw an error Uncaught ReferenceError: span is not defined

After some research, I found out that I need to use a alternative notatio using bracket. ['life-span']

this.setState({
  newArtist: {
    birthdate: artist['life-span']begin
  }
});

But this one make Babel to throw a syntax error.

bundle.js:1 SyntaxError: /file/path: Unexpected token, expected , (24:38) while parsing file

So I'm stuck here.

My babel configuration is quite lite, only use the es2015 & react preset.

Any idea what it could be ?

like image 252
Dry-macarony Avatar asked Oct 26 '25 16:10

Dry-macarony


1 Answers

birthdate: artist['life-span'].begin

instead of birthdate: artist['life-span']begin

like image 77
François Richard Avatar answered Oct 29 '25 06:10

François Richard