- Interactions with Sitecore
Save and restore app URL state
Version: 0.5
Required extension points: any
Required SDK packages: client
Required API access: none
Use application.urlState to save a view in the host URL and restore it after a reload or when opening a shared link.
Before you begin
- Complete an SDK quick start using
clientversion0.5.0or later. - Review how app URL state works.
- Review the example
application.urlStatequery and mutation.
Restore a saved view
To restore a view in the component where you run useMarketplaceClient:
-
Update your imports and define the view type and a validator outside the component. Use a
typealias, not aninterface, to ensure the view is compatible withJsonObject: -
Inside the component, add state and an Effect Hook that queries the host after the SDK connects:
The validator checks the saved values before using them. The ref prevents a late response from overwriting a view the user has already changed.
Save or clear a view
- In the same component, add a function to save a complete view object or clear it with
null: - Add controls to your existing return statement, then save your changes:
This adds a Show SEO button and a Reset view button to your app's UI. If you add other controls, set
hasChanged.current = truewhen the user changes the view.
Test saving and restoring a view
- Open your Marketplace app in one of its extension points and click Show SEO. Wait for the host URL to update, then reload the page. The view shows seo and 30d. This shows that the app URL state is successfully restored.
- Click Reset view, wait for the URL update, then reload. The view shows overview and 7d. This shows that the app URL state is successfully cleared.
If you have suggestions for improving this article, let us know!