MedplumClient Lifecycle Events | Medplum

Events

The MedplumClient extends the EventTarget class and emits several events that can be listened to for insight into state changes around auth and other changes in the internals of MedplumClient. These are usually used for triggering rerenders within the context of React, but may also be useful for debugging purposes.

Working with the MedplumClient Lifecycle in React

When wrapping your app in a MedplumProvider, you should be aware of the lifecycle events of MedplumClient. Each of the lifecycle events will trigger a rerender at the root of your app; so your root component (what we call by convention in our examples App.tsx) should resilient to rerenders. This can be accomplished in most cases with the following pattern:

function App() {

const profile = useMedplumProfile();

const navigate = useNavigate();

// Use this to guard against initial load

// After initial load, this code path will always be skipped over

if (medplum.isLoading()) {

return <Loading />;

}

// Use this if you want to unmount the entire app and show a spinner when your profile changes

// This will cause remount on switching profiles

// Or an unmount whenever token refresh fails and a profile is no longer present

if (!profile) {

// Redirect users back to signin whenever there is no profile anytime after initial load

navigate('/signin');

return null;

}

return <RestOfYourApp />;

}

medplum.isLoading() vs useMedplumContext().loading

Historically medplum.isLoading() and useMedplumContext().loading signalled slightly different things, born out of the special-case of using MedplumClient with ExpoClientStorage for React Native, where there was a separate step of initializing the storage asynchronously before authentication could take place. However, both of these values should now always be in sync with each other, and they can be used interchangeably as of version 3.1.9.