React adapter helpers for yii2-framework/inertia
React-friendly root view and Vite asset integration for Yii2 Inertia applications
yii2-framework/inertia-react is a thin PHP-side adapter package for building React-based Inertia applications on top
of yii2-framework/inertia.
This package does not install npm dependencies for you. Instead, it provides.
- a React-specific bootstrap class for Yii2;
- a default root view that outputs Vite tags plus the initial Inertia page payload;
- a Vite helper component for development-server and manifest-driven production assets;
- React Refresh preamble output for
@vitejs/plugin-reactin development mode; - documentation and conventions for the application-owned React client entrypoint.
composer require yii2-framework/inertia-react:^0.1Register the React bootstrap class.
return [
'bootstrap' => [
\yii\inertia\react\Bootstrap::class,
],
'components' => [
'inertiaReact' => [
'class' => \yii\inertia\Vite::class,
'baseUrl' => '@web/build',
'devMode' => YII_ENV_DEV,
'devServerUrl' => 'http://localhost:5173',
'entrypoints' => [
'resources/js/app.jsx',
],
'manifestPath' => '@webroot/build/.vite/manifest.json',
'preambleProvider' => \yii\inertia\react\Bootstrap::reactRefreshPreambleProvider(),
],
],
];Use only yii\inertia\react\Bootstrap::class in the bootstrap list. It already delegates the base
yii2-framework/inertia bootstrap.
Install the client-side dependencies in your application project.
npm install react react-dom @vitejs/plugin-react @inertiajs/react viteThen create your client entrypoint, for example resources/js/app.jsx:
import { createInertiaApp } from "@inertiajs/react";
import { createElement } from "react";
import { createRoot } from "react-dom/client";
createInertiaApp({
resolve: (name) => {
const pages = import.meta.glob("./Pages/**/*.jsx", { eager: true });
return pages[`./Pages/${name}.jsx`];
},
setup({ el, App, props }) {
createRoot(el).render(createElement(App, props));
},
});When devMode is enabled and preambleProvider is set to
\yii\inertia\react\Bootstrap::reactRefreshPreambleProvider(), this package emits the React Refresh preamble
recommended by the official Vite backend integration guide before loading @vite/client and your entrypoint scripts.
This package expects a Vite manifest file generated with build.manifest = true. In production it will render.
- style sheet tags for the entrypoint chunk and its imported chunks;
- module entry scripts for each entrypoint;
- optional
modulepreloadtags for imported JavaScript chunks.
For detailed configuration options and advanced usage.