1

So I have this component

import React from 'react';
import ReactDOM from 'react-dom';

import NeoHeader from './header/NeoHeader';
import NeoLoginModal from './modal/NeoLoginModal';

class Neo extends React.Component {
    constructor(props) {
        super(props);
        this.state = {loginModal: false};
    }
    render() {
        return( <NeoHeader/> { this.state.loginModal ? <NeoLoginModal /> : null })
    }
}

ReactDOM.render(
  <Neo/>,
  document.getElementById('react-wrapper')
);

and as you can see, I'm trying to show the component NeoLoginModal when a state prop is set to true. However, building with Laravel mix gives an unexpected token error at the {this..} start. This is documented in multiple places as a correct way to do this, so what's the error about?

1 Answer 1

2

The problem is not within the Laravel Mix but with your component HTML structure. In React you can't render siblings as first-level elements. In order to make your code work you should wrap both child components with a parent tag, for example:

render() {
    return (
        <div>
            <NeoHeader />
            { this.state.loginModal ? <NeoLoginModal /> : null }
        </div>
    );
}
Sign up to request clarification or add additional context in comments.

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.