2

I'm getting an error for the following code:

{this.props.mountedPage === "frontpage" ?
            <Navbar className="navbar-nomargin"> :
            <Navbar>}

Error message: Error - Parsing error: Unexpected token .
Refers to the first dot in this.props.mountedPage

I have something similar a few lines down and it's working fine without any lint errors:

{this.props.mountedPage === "frontpage" ?
                    <NavItem href="/#services">Tjänster</NavItem>
                    : null}

EDIT:
Thanks for the suggestions, you were right, the linter failed to close the tag properly. I ended up moving the rendering of elements to a separate function so that the tag would close properly:

render: function() {
    return (
        this.props.mountedPage == "frontpage" ?
        <Navbar className="navbar-nomargin">{this.renderBody()}</Navbar> :
        <Navbar>{this.renderBody()}</Navbar>
    );
}

3 Answers 3

2

The syntax error is an error with your JSX. All JSX tags have to be manually closed so change your navbar tags.

<navbar> // this will throw a syntax error in JSX

these will not:

<navbar /> 

OR

<navbar></navbar>

EDIT: Conditionally rendering a whole different component in this case is not the ideal solution because the only difference is a className. You can do this to just conditionally change the className:

<navbar className={ this.props.mountedPage === 'frontpage' ? 'navbar-nomargin' : '' } />

Or you can use this npm package classnames which I find to be extremely useful in any React application because this is such a common thing to do.

import classNames from 'classnames'

...
<navbar className={ classNames('navbar', {
    'navbar-nomargin': mountedPage === 'frontPage'
}) } />

It is very useful because you can list any class names you want (like the 'navbar' class I added) and you can also include an object to handle all your conditional classes with this structure:

{
    desiredClassName: boolean,
    anotherPossibleClass: boolean,
}

This allows you to put the conditional as the value of the object and the className as the key. true will result in the className including the key.

Sign up to request clarification or add additional context in comments.

Comments

2

You are missing closing tags on your NavBar. NavItem has a closing tag, so it works.

{this.props.mountedPage === "frontpage" ?
        <Navbar className="navbar-nomargin" /> :
        <Navbar /> }

Comments

0

There are two ways you can achieve this

Option 1:

class MyComponent extends React.Component {
  render() {
    return (      
      this.props.mountedPage === "frontpage" ?
      <h1>asdad</h1> :
      <h1/>
    );
  }
}

React.render(
  <MyComponent mountedPage="frontpage" name="Joe Schmoe"/>,
  document.getElementById('react_example')
);

Option 2:

class MyComponent extends React.Component {
  render() {
    return (
      <div>
      {
        this.props.mountedPage === "frontpage" ?
        <h1>asdad</h1>:
        <h1/>
      }
      </div>
    );
  }
}

React.render(
  <MyComponent mountedPage="frontpage" name="Joe Schmoe"/>,
  document.getElementById('react_example')
);

And also remember to close the HTMLtags properly. Hope this helps

Comments

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.