Kritim Yantra
Jun 10, 2025
Imagine building a car: Laravel is the engine (backend logic), React is the sleek dashboard (frontend), and React Router is the GPS (navigation). Together, they let you create fast, interactive web apps without reloading pages. Intrigued? Let’s build one!
By the end, you’ll:
1️⃣ Create a Laravel 12 app with React.
2️⃣ Set up React Router for smooth navigation.
3️⃣ Connect Laravel routes to React’s SPA (Single-Page App).
composer create-project laravel/laravel laravel-react-app
cd laravel-react-app
npm install react react-dom @vitejs/plugin-react
Update 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 resources/js/app.js
to app.jsx
and add:
// resources/js/app.jsx
import React from 'react';
import ReactDOM from 'react-dom/client';
function App() {
return <h1>Hello from React + Laravel 12!</h1>;
}
ReactDOM.createRoot(document.getElementById('app')).render(<App />);
Modify resources/views/welcome.blade.php
:
<!DOCTYPE html>
<html>
<body>
<div id="app"></div>
@vite('resources/js/app.jsx')
</body>
</html>
✅ Test It: Run npm run dev
+ php artisan serve
. You’ll see your React component!
npm install react-router-dom
resources/js/pages/Home.jsx
: export default function Home() { return <h2>🏠 Home Page</h2>; }
resources/js/pages/About.jsx
: export default function About() { return <h2>📖 About Us</h2>; }
Update resources/js/app.jsx
:
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
import Home from './pages/Home';
import About from './pages/About';
function App() {
const router = createBrowserRouter([
{ path: "/", element: <Home /> },
{ path: "/about", element: <About /> },
]);
return <RouterProvider router={router} />;
}
🚦 Test Routing:
http://localhost:8000
→ See "Home Page". http://localhost:8000/about
→ Oops! Laravel blocks it.Edit routes/web.php
:
<?php
use Illuminate\Support\Facades\Route;
Route::get('/{any}', function () {
return view('welcome'); // Your Blade file with React
})->where('any', '.*'); // Catch ALL routes
Why?
✅ Test Again:
/about
→ Now shows the About page! 🎉/about
: Laravel’s catch-all route serves welcome.blade.php
, then React Router renders the About component.Traditional Site | SPA (Our Setup) |
---|---|
Page reloads on navigation. | Feels like a mobile app (fast!). |
Backend handles every route. | Frontend manages routing. |
api/user
). lazy()
.You just built a Laravel 12 + React SPA with client-side routing! Recap:
Questions? Drop them below! 👇
Transform from beginner to Laravel expert with our personalized Coaching Class starting June 20, 2025. Limited enrollment ensures focused attention.
1-hour personalized coaching
Build portfolio applications
Industry-standard techniques
Interview prep & job guidance
Complete your application to secure your spot
Thank you for your interest in our Laravel mentorship program. We'll contact you within 24 hours with next steps.
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google