Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Emokores's avatar

Inertia shared data not passed to the frontend - Inertia with React

I have an application that has team management. I would like to display the teams in the user profile dropdown but I keep getting the error in the console: Uncaught TypeError: Cannot read properties of undefined (reading 'all') when I try to map through the array. This is what I have in my HandleInertiaRequests middleware:


public function share(Request $request)
    {
        return array_merge(parent::share($request), [
           //* showing teams
            'teams' => [
                'all' => Team::orderByDesc('created_at')->get(),
                'current' => auth()->user()->current_team ?? null,
            ],
           
        ]);
    }

So, in my frontend, I access it as a prop in the export function UserSettings ({ teams }) and I loop through with teams.all.map(), I already have a team registered in my teams table and a team attached to the authenticated user in my team_user pivot table. I don't know what I am doing wrong, yet all the other shared data is being passed.

0 likes
10 replies
Sinnbeck's avatar

Show the component. Are you sure it's a page and not a sub component?

Emokores's avatar

@Sinnbeck I am trying to pass the data to a subcomponent. I have a component that is made up of two subcomponents: notification component (which works well and data is being passed to it) and user options component (the problem is here)

Sinnbeck's avatar

@Emokores sub components does not get them automatically. That's what usePage() is for

Emokores's avatar

@Sinnbeck My notification component is a subcomponent and all the data is being passed to it properly as expected. How can I use usePage() in this case, because I'm trying to display shared data?

Sinnbeck's avatar

@Emokores just add this in it

import { usePage } from '@inertiajs/inertia-react'

const {teams} = usePage().props 
1 like
Emokores's avatar

@frankielee Thanks! This has solved it. Though I don't understand why it worked in the other components without usePage()

Elliot69's avatar

Passing this data manually in each response isn't practical. In these situations shared data can be useful. Sharing data. The server-side adapters provide a way.

Please or to participate in this conversation.