1

所以我有这个组件

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')
);

正如你所看到的,当 state 属性设置为 true 时,我试图显示组件 NeoLoginModal。但是,使用 Laravel mix 进行构建在{this..}开始时会出现意外的令牌错误。这在多个地方被记录为正确的方法,那么错误是什么?

4

1 回答 1

2

问题不在于 Laravel Mix,而在于您的组件 HTML 结构。在React你不能将兄弟姐妹渲染为第一级元素。为了使您的代码正常工作,您应该使用父标签包装两个子组件,例如:

render() {
    return (
        <div>
            <NeoHeader />
            { this.state.loginModal ? <NeoLoginModal /> : null }
        </div>
    );
}
于 2017-05-16T22:40:31.493 回答