Storing and globally accessing user information
In this section, we'll configure the plugin-initial-state
plugin to store and globally access user information.
To configure the initial state, we only need to create a function named getInitialState
in the app.tsx
file. The getInitialState
function will be executed before React renders the entire application, and its return value will be used as the global state. We can use the @@initialState
model to access the values.
Let's configure the initial state by following these steps:
- Create a new file called
globalState.d.ts
in thetypes
folder, and create theGlobalState
interface as follows:import { User } from '@/types/user.d'; export interface GlobalState { login?: (email: string, password: string) => Promise<User>; logout?: () => Promise<void>; fetchUser?: () => Promise<User>; currentUser?: User; }
...