Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way (if possible) to store JSX code into a Javascript variable

I´ve written the following code using ReactJs´s JSX syntax:

import { Link } from 'react-router';

class SidebarMenuItem extends React.Component {

render() {

    var href = (this.props.submenu ? 'javascript:' : {<Link to="mod/admin" + this.props.link />};

    return ( 

        <a href={href} onClick={this.selected}>
            <i className={'fa ' + this.props.icon} />
            <span>{this.props.title}</span>
        </a>

    )
  }
}

But it seend that I cannot store a direct JSX code into a variable, as I got the following error:

Module build failed: SyntaxError: D:/9. DEV/client/components/App/SidebarMenuItem.js: Unexpected token, expected , (41:52)

  40 | 
> 41 |      var href = (this.props.submenu ? 'javascript:' : {<Link to="mod/admin" + this.props.link />};
     |                                                        ^

What is the correct way to store my Link component in the href variable ?

like image 858
Mendes Avatar asked Jan 30 '17 23:01

Mendes


2 Answers

Use () around JSX that spans multiple lines

var href = this.props.submenu ? 'javascript:' : (<Link to="mod/admin" + this.props.link />);
like image 96
kamikazeOvrld Avatar answered Oct 20 '22 17:10

kamikazeOvrld


you would just write plain jsx, in your case you need to remove the brackets around the jsx, and then include the brackets around the "mod/admin" + this.props.link portion just like you would when you write in the render method.

var href = this.props.submenu ? 'javascript:' : <Link to={"mod/admin" + this.props.link} />
like image 42
finalfreq Avatar answered Oct 20 '22 16:10

finalfreq