简体   繁体   中英

react-laravel Unexpected token

I am trying to get react-laravel working. I have installed everything correctly (v8js etc) but I keep getting:

V8Js::executeString():18903: SyntaxError: Unexpected token <

The problem seems to be with my component.js file:

var Application = React.createClass({
    render: function() {
        return (
            <div className="Networks">
                <ul>
                    <li>Test</li>
                </ul>
            </div>
        );
    }
});

How can I fix this?


Also, is it possible to use ES6?


My view:

<html>

<head>
    <title>JSPM Experiment</title>
    <script src="{{ asset('vendor/react-laravel/react.js') }}"></script>
    <script src="{{ asset('js/components.js') }}"></script>
    <script src="{{ asset('vendor/react-laravel/react_ujs.js') }}"></script>
</head>

<body>

<div id='react-root'>
</div>
@react_component('Application',[ 'title' => 'Hello, World' ], [ 'prerender' => true ])
</body>

</html>

Is it "component.js" or "components.js"? You likely have a typo.

Usually you get SyntaxError: Unexpected token < when trying to load a javascript file that doesn't exist. Laravel renders a 404 HTML page and the first < trips up your script tag.

In case someone has still this error, I installed Laravel 6 and set up React:

php artisan preset react
npm install

And then I got a similar error "Syntax Error: Unexpected token" as the React JSX syntax didn't seem to be understood. It got fixed by altering babel config in webpack.mix.js :

mix.babelConfig({
  "presets": [
    "@babel/preset-react",
  ],
});

If you have problem with Laravel 6. Please check your webpack.mix.js file, it should be mix.react() instead of mix.js()

https://laravel.com/docs/6.x/mix#react

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM