No need babel, you can use vite. This is for Laravel 11
Install react
npm install --save-dev @vitejs/plugin-react
Modify vite.config.js
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [
laravel(['resources/js/app.jsx']),
react(),
],
});
Rename resource/js/app.js to app.jsx
Import react components or react main App.jsx file in to the app.jsx file.
Create blade a file. use <div id="root"></div> to render react app.
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
<!-- Styles / Scripts -->
@viteReactRefresh
@vite('resources/js/app.jsx')
</head>
<body class="font-sans antialiased dark:bg-black dark:text-white/50">
<div id="root"></div>
</body>
</html>
- Go to web.php and add route. (This route will help to render correct page/component in react app instead of returning Laravel 404 not found error)
<?php
use Illuminate\Support\Facades\Route;
Route::get('/{any?}', function () {
return view('app');
})->where('any', '^(?!api\/)[\/\w\.\,-]*');
- Finally
php artisan serve
npm run dev
Laravel 11 with vite