Ad

Reactjs Unknown: Unexpected Token On React.createElement

- 1 answer

I've got a .jsx file and I'm writing in es6. I have a component and I want it to return a good ole link! I copied the exact link from the React Documentation. Here is my code...

class PanelContent extends React.Component { 
  render() {
    return (
      React.createElement('a', {href: 'https://facebook.github.io/react/'}, 'Hello!')
    )
  }
}

And the error I'm receiving is, SyntaxError: unknown: Unexpected token (4:35). The error is pointing at the : in href. So please anyone help.

Ad

Answer

By using the parenthesis in the return of the render function react thinks you're going to be writing in jsx if you don't want to inline your html you could do: EDIT: This first example seems like it should work with or without parenthesis

class PanelContent extends React.Component { 
  render() {
    return React.createElement('a', {href: 'https://facebook.github.io/react/'}, 'Hello!')
  }
}

or if you want to use jsx (make sure you have a processor running):

class PanelContent extends React.Component { 
  render() {
    return (
      <a href='https://facebook.github.io/react/'>Hello!</a>
    ) 
  }
}
Ad
source: stackoverflow.com
Ad