Skip to content

Commit 2935587

Browse files
committed
update react example
1 parent 82f5b38 commit 2935587

File tree

1 file changed

+21
-31
lines changed

1 file changed

+21
-31
lines changed

frontend.md

+21-31
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
- [PHP and Blade](#php-and-blade)
66
- [Livewire](#livewire)
77
- [Starter Kits](#php-starter-kits)
8-
- [Using Vue / React](#using-vue-react)
8+
- [Using React or Vue](#using-react-or-vue)
99
- [Inertia](#inertia)
1010
- [Starter Kits](#inertia-starter-kits)
1111
- [Bundling Assets](#bundling-assets)
@@ -104,19 +104,19 @@ If you're new to Laravel, we recommend getting familiar with the basic usage of
104104

105105
If you would like to build your frontend using PHP and Livewire, you can leverage our [Livewire starter kit](/docs/{{version}}/starter-kits) to jump-start your application's development.
106106

107-
<a name="using-vue-react"></a>
108-
## Using Vue / React
107+
<a name="using-react-or-vue"></a>
108+
## Using React or Vue
109109

110-
Although it's possible to build modern frontends using Laravel and Livewire, many developers still prefer to leverage the power of a JavaScript framework like Vue or React. This allows developers to take advantage of the rich ecosystem of JavaScript packages and tools available via NPM.
110+
Although it's possible to build modern frontends using Laravel and Livewire, many developers still prefer to leverage the power of a JavaScript framework like React or Vue. This allows developers to take advantage of the rich ecosystem of JavaScript packages and tools available via NPM.
111111

112-
However, without additional tooling, pairing Laravel with Vue or React would leave us needing to solve a variety of complicated problems such as client-side routing, data hydration, and authentication. Client-side routing is often simplified by using opinionated Vue / React frameworks such as [Nuxt](https://nuxt.com/) and [Next](https://nextjs.org/); however, data hydration and authentication remain complicated and cumbersome problems to solve when pairing a backend framework like Laravel with these frontend frameworks.
112+
However, without additional tooling, pairing Laravel with React or Vue would leave us needing to solve a variety of complicated problems such as client-side routing, data hydration, and authentication. Client-side routing is often simplified by using opinionated React / Vue frameworks such as [Next](https://nextjs.org/) and [Nuxt](https://nuxt.com/); however, data hydration and authentication remain complicated and cumbersome problems to solve when pairing a backend framework like Laravel with these frontend frameworks.
113113

114114
In addition, developers are left maintaining two separate code repositories, often needing to coordinate maintenance, releases, and deployments across both repositories. While these problems are not insurmountable, we don't believe it's a productive or enjoyable way to develop applications.
115115

116116
<a name="inertia"></a>
117117
### Inertia
118118

119-
Thankfully, Laravel offers the best of both worlds. [Inertia](https://inertiajs.com) bridges the gap between your Laravel application and your modern Vue or React frontend, allowing you to build full-fledged, modern frontends using Vue or React while leveraging Laravel routes and controllers for routing, data hydration, and authentication — all within a single code repository. With this approach, you can enjoy the full power of both Laravel and Vue / React without crippling the capabilities of either tool.
119+
Thankfully, Laravel offers the best of both worlds. [Inertia](https://inertiajs.com) bridges the gap between your Laravel application and your modern React or Vue frontend, allowing you to build full-fledged, modern frontends using React or Vue while leveraging Laravel routes and controllers for routing, data hydration, and authentication — all within a single code repository. With this approach, you can enjoy the full power of both Laravel and React / Vue without crippling the capabilities of either tool.
120120

121121
After installing Inertia into your Laravel application, you will write routes and controllers like normal. However, instead of returning a Blade template from your controller, you will return an Inertia page:
122122

@@ -137,41 +137,31 @@ class UserController extends Controller
137137
*/
138138
public function show(string $id): Response
139139
{
140-
return Inertia::render('Users/Profile', [
140+
return Inertia::render('users/show', [
141141
'user' => User::findOrFail($id)
142142
]);
143143
}
144144
}
145145
```
146146

147-
An Inertia page corresponds to a Vue or React component, typically stored within the `resources/js/Pages` directory of your application. The data given to the page via the `Inertia::render` method will be used to hydrate the "props" of the page component:
147+
An Inertia page corresponds to a React or Vue component, typically stored within the `resources/js/pages` directory of your application. The data given to the page via the `Inertia::render` method will be used to hydrate the "props" of the page component:
148148

149-
```vue
150-
<script setup>
151-
import Layout from '@/Layouts/Authenticated.vue';
152-
import { Head } from '@inertiajs/vue3';
149+
```jsx
150+
import Layout from '@/layouts/authenticated';
151+
import { Head } from '@inertiajs/react';
153152

154-
const props = defineProps(['user']);
155-
</script>
156-
157-
<template>
158-
<Head title="User Profile" />
159-
160-
<Layout>
161-
<template #header>
162-
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
163-
Profile
164-
</h2>
165-
</template>
166-
167-
<div class="py-12">
168-
Hello, {{ user.name }}
169-
</div>
170-
</Layout>
171-
</template>
153+
export default function Show({ user }) {
154+
return (
155+
<Layout>
156+
<Head title="Welcome" />
157+
<h1>Welcome</h1>
158+
<p>Hello {user.name}, welcome to Laravel and Inertia.</p>
159+
</Layout>
160+
)
161+
}
172162
```
173163

174-
As you can see, Inertia allows you to leverage the full power of Vue or React when building your frontend, while providing a light-weight bridge between your Laravel powered backend and your JavaScript powered frontend.
164+
As you can see, Inertia allows you to leverage the full power of React or Vue when building your frontend, while providing a light-weight bridge between your Laravel powered backend and your JavaScript powered frontend.
175165

176166
#### Server-Side Rendering
177167

0 commit comments

Comments
 (0)