Setup Laravel 5.4 to use React

前端 未结 2 809
死守一世寂寞
死守一世寂寞 2021-02-05 10:50

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 Lar

2条回答
  •  遥遥无期
    2021-02-05 11:38

    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!

提交回复
热议问题