15

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 ?

1
  • Can you please elaborate more about what you're trying to achieve? href attribute expects a string. Or you put <Link> as a replacement for <a>. And your code is syntactically wrong. Commented Jan 30, 2017 at 23:17

2 Answers 2

15

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} />
Sign up to request clarification or add additional context in comments.

Comments

7

Use () around JSX that spans multiple lines

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

2 Comments

Sorry about that, I actually hadn't closely seen what exactly you were trying to achieve, what exactly are you trying to assign to href? You also have an opening parenthesis after the = sign, and you never close it
Parentheses aren't required to split JSX tags across multiple lines.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.