composer global require laravel/installer
Once Laravel is installed, you can create a new Laravel project:
laravel new my-project
composer require laravel/ui
php artisan ui:publish
resources/js
directory. Open the file and add the following code to the top of the file:
import React from 'react';
import ReactDOM from 'react-dom';
resources/js/components
directory and name it Welcome.js
. Add the following code to the file:
npm install react react-dom
import React from 'react';
const Welcome = () => {
return (
<div>
<h1>Welcome to Laravel with React!</h1>
</div>
);
};
export default Welcome;
import Welcome from './components/Welcome';
ReactDOM.render(<Welcome />, document.getElementById('root'));
php artisan serve
Now, you can visit http://localhost:8000
in your web browser to see your React component.
To create a new React route in Laravel, you can follow these steps:
routes/web.php
file.
Route::get('/react', function () {
return view('welcome');
});
This route will render the welcome.blade.php
view, which is located in the resources/views
directory.
welcome.blade.php
file.
<html>
<head>
<title>Laravel with React!</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
http://localhost:8000/react
in your web browser to see your React component.