5

I'm using the Reactjs in Laravel.Here I made some changes in my React Component and when I refresh my browser then changes are not shown.

Files:

  1. resources/views/welcome.blade.php

    <!doctype html>
    <html lang="{{ app()->getLocale() }}">
        <head>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <title>arjunphp.com | Laravel 5.6 with React JS</title>
            <link href="{{asset('css/app.css')}}" rel="stylesheet" type="text/css">
        </head>
        <body>
            <div id="example"></div>
            <script src="{{asset('js/app.js')}}" ></script>
        </body>
    </html>
    
  2. resources/asset/js/app.js

    /**
     * First we will load all of this project's JavaScript dependencies which
     * includes React and other helpers. It's a great starting point while
     * building robust, powerful web applications using React + Laravel.
     */
    
    require('./bootstrap');
    
    /**
     * Next, we will create a fresh React component instance and attach it to
     * the page. Then, you may begin adding components to this application
     * or customize the JavaScript scaffolding to fit your unique needs.
     */
    
    require('./components/Example');
    
  3. resources/asset/js/components/Example.js

    import React, { Component } from 'react';
    import ReactDOM from 'react-dom';
    import Button from '@material-ui/core/Button'
    
    export default class Example extends Component {
        render() {
            return (
                <div className="container">
                    <div className="row">
                        <div className="col-md-8 col-md-offset-2">
                            <div className="panel panel-default">
                                <div className="panel-heading">Example Component</div>
    
                                <div className="panel-body">
                                    I'm an example component tomas!
                                </div>
                                <Button size="small" color="primary" href="#" target="_blank">
                                    Go To Course
                                </Button>
                            </div>
                        </div>
                    </div>
                </div>
            );
        }
    }
    
    if (document.getElementById('example')) {
        ReactDOM.render(<Example />, document.getElementById('example'));
    }
    

I add a Button in this component when I refresh the browser the changes are not shown in browser.

Screen-Shot:

enter image description here

NearHuscarl
  • 66,950
  • 18
  • 261
  • 230
Tomas
  • 514
  • 4
  • 13
  • 37
  • 1
    probably (depending on your setup) you have to run "npm run dev" before you see any changes... probably (depending on your setup) you will find the available npm commands in the package.json file – herrjeh42 Nov 22 '18 at 06:49
  • Use `npm run watch --watch-poll` command after starting your developing and NPM will listen and re-build all your JS components on the fly after any changes. – alexey-novikov Nov 22 '18 at 07:09
  • Please how did you solve this? I am having similar issue. – shekwo Sep 19 '19 at 18:31
  • Had similar problem, clearing cache worked for me. – David.E Apr 06 '23 at 22:27

6 Answers6

12

Did you compile react.js code?

If not run:

npm run dev

If you don't see changes, clear the browser cache.

Laravel Documentation

Remember, you should run the npm run dev command each time you change a Vue component. Or, you may run the npm run watch command to monitor and automatically recompile your components each time they are modified.

It's needed for React too.

Gass
  • 7,536
  • 3
  • 37
  • 41
Puria Golgoon
  • 216
  • 1
  • 5
8

Try this method, it's called Live Reloading.

With Live Reloading, you can get refreshed browser when you save your .js file.

  1. Go to webbpack.mix.js and add this at the bottom:

    mix.browserSync('localhost:8000');

The parameter inside depend on your php artisan serve url and port.

  1. Run npm run watch to install dependency needed for browserSync method.
  2. Run php artisan serve and npm run watch (on different cmd)

Hope this help.

Don't forget to give applause if it is work.

source: https://medium.com/@michael.tandio/easy-setup-hot-loader-for-react-laravel-with-laravel-mix-8f68a1393624

Michael Harley
  • 831
  • 1
  • 9
  • 20
2

For update your code after any change in your file, run this command

npm run watch

E.outmane
  • 51
  • 2
0

if you are using chrome. open incognito tab and use the incognito tab it clears cache automatically on reload

0

Step 1: firstly you must write on the terminal:

npm install
npm run dev

Step 2: after that time, whenever you update anything just write:

npm run watch

I think everything ok

NH Dalim
  • 312
  • 1
  • 5
  • 13
-1

Just run this command in the console:

npm run 

the browser will now automatically get updated corresponding to file changes