

Installing Inertia — React in Laravel Project
source link: https://blog.geekyants.com/installing-inertia-react-in-laravel-project-79268603b742
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Installing Inertia — React in Laravel Project
Let’s learn how to install Inertia in a Laravel project with React
Inertia ships with official server-side adapters for Laravel. In this article, we will install Inertia.js in Laravel with React.js.
First, we will perform server-side installation and then continue with the client-side installation.
Step 1 — Install the Inertia package in your Laravel project
Run the command below to install the Inertia package in your project via composer:
composer require inertiajs/inertia-laravel
Step 2 — Add root template
Create a new file, app.blade.php in resources/views and paste the below code. This file will be loaded on the first page visit. This will be used to load your site assets (CSS and JavaScript), and will also contain a root <div> to boot your JavaScript application in.
Step 3 — Setup middleware
Next, setup the inertia middleware. Run the below command to do the same.
php artisan inertia:middleware
Once generated, register the HandleInertiaRequests middleware in your App\Http\Kernel, as the last item in your web middleware group.
'web' => [
// ...
\App\Http\Middleware\HandleInertiaRequests::class,
],
Now, our server-side installation is done.
Next, we will perform the client-side installation.
Step 4 — Install client-side dependencies
Install the Inertia client-side adapters using NPM or Yarn.
npm install @inertiajs/inertia @inertiajs/inertia-react
yarn add @inertiajs/inertia @inertiajs/inertia-react
Step 5 — Initialize app
Next, update your main JavaScript file app.js in resources/js/app.js. If you have existing app.js in that folder, rename the old file, create a new app.js, and write the below code in that file. What we’re doing here is initializing the client-side framework with the base Inertia component.
Step 6 — Install babel plugins
Why do we need Babel? Code splitting breaks apart the various pages of your application into smaller bundles, which are then loaded on demand when visiting new pages. This can significantly reduce the size of the initial JavaScript bundle, improving the time first to render. To use code splitting with Inertia, you’ll need to enable dynamic imports. That’s why you’ll need a Babel plugin to make this work.
Run the below commands to install the babel plugins:
npm install @babel/plugin-syntax-dynamic-import
yarn add @babel/plugin-syntax-dynamic-import
Next, create a new file `.babelrc` in your project and add the following code.
Step 7 — Create a Test.js component
Now, create a new Test.js component in `resources/js/Pages` for testing if our integration is successful.
Step 8 — Make controller and route
To render the component, make TestController and add a new route in web.php.
Conclusion
That’s it.
Try to call the route project_url/test, and your inertia with React in Laravel integration is done.
Hope you found this article helpful.
This article was written by Aanchal Goyal, Software Engineer at GeekyAnts.
Recommend
-
105
readme.md Inertia.js Note: This project is in the very early stages of development and IS NOT yet intended for public con...
-
247
readme.md Inertia.js Laravel Adapter Note: This project is in the very early stages of development and IS NOT yet intende...
-
193
readme.md Inertia.js Vue Adapter Note: This project is in the very early stages of development and IS NOT yet intended fo...
-
52
Core conceptsInstallationThe basicsAdvancedJavaScript apps the monolith wayInertia is a new approach to building classic server-driven web apps. We call it the modern monolith....
-
8
Covid Kills Inertia: Homeownership Edition 2 months ago You may have contemplated, or been asked at some point: “How will homeo...
-
6
23 Jan 19Introducing Inertia for Popmotion PureToday, we’re releasing the first new animation for Popmotion Pure in over a year!Inertia combines the best of the
-
10
Available optionsWorking with Vue.js gives us more flexibility. We can easily take advantage of existing libraries such as vue-js-modal or creating our own client side one. However,...
-
7
-
12
-
10
Inertia - Server-side Rendering (SSR) 以 React 為範例 本文為官方文件翻譯,如使用其他前端框架可參考官方 SSR 支援提前渲染造訪的頁面,並且回傳渲...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK