Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setup Laravel 5.4 to use React

I am not new to Laravel but am new to JavaScript frameworks.

I want to use React in a laravel build but cannot find any information on how to do this.

I have Laravel 5.4. I know this comes pre-packed with Vue and Laravel Mix instead of Gulp.

I am following the tutorial here: https://blog.tighten.co/adding-react-components-to-a-laravel-app but this is not meant for 5.4. It mentions needing to update the gulpfile but, since Laravel 5.4 doesn't use Gulp, there isn't one!

Can someone please help with a small tutorial or a couple of steps that I need to replicate in order to get React working in 5.4?

I have Googled but can only find NPM links, which are less than useful!

like image 733
Wildcard27 Avatar asked Apr 05 '17 10:04

Wildcard27


People also ask

Can I integrate React with Laravel?

It's still possible and very straightforward to set up Vue or React with a Laravel project. In this article, I'll walk you through the process of bootstrapping a brand new Laravel project and integrating React with it so all Laravel developers, as well as React.

Is Laravel and React a good combo?

In Conclusion, js and React. js, integrating Laravel and VueJS can help you get the best of both worlds. Both Laravel and Vue. js frameworks separately contribute to some of the major web applications in the world, and bringing them together can bring phenomenal results in the application development space.

Can you install React with NPM?

NodeJS and NPM are the platforms need to develop any ReactJS application. You can install NodeJS and NPM package manager by the link given below. You can install React using npm package manager by using the below command. There is no need to worry about the complexity of React installation.


2 Answers

Thanks to Joe for pointing out the part of the docs that I needed. There was a little more to it so will describe my steps below to help anyone else out.

  1. Delete the node_modules folder. This isn't necessary for brand new builds but if you have followed any other tutorials and installed things you don't need, this will save a few headaches. If you don't have a node_modules folder, ignore this step.

    Please note that the above step will remove anything you have installed using NPM.

  2. Run npm install from your project root. This will install all NPM components used by Laravel.

  3. Open webpack.mix.js and change:

    mix.js('resources/assets/js/app.js', 'public/js') 
        .sass('resources/assets/sass/app.scss', 'public/css');
    

    to

    mix.react('resources/assets/js/app.js', 'public/js')
        .sass('resources/assets/sass/app.scss', 'public/css');
    
  4. Run npm run dev to compile the Javascript from the lesson contained in the question above. Your React JS outputs should now be working.

  5. (optional) Use npm run watch to compile on the fly.

I really hope this helps someone else out as much as it helped me!

like image 117
Wildcard27 Avatar answered Oct 05 '22 20:10

Wildcard27


According to the docs, React support is built in to Mix:

Mix can automatically install the Babel plug-ins necessary for React support. To get started, replace your mix.js() call with mix.react():

mix.react('resources/assets/js/app.jsx', 'public/js');

Behind the scenes, Mix will download and include the appropriate babel-preset-react Babel plug-in.

I'm not 100% sure whether or not this'll automatically include React in your output bundle - my guess would be no (in which case you'll have to use a normal mix.js() call to pull it in).

like image 25
Joe Clay Avatar answered Oct 05 '22 22:10

Joe Clay