Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Merge new SSR state on client-side navigation #154

Closed
DAreRodz opened this issue Feb 20, 2023 · 1 comment
Closed

Merge new SSR state on client-side navigation #154

DAreRodz opened this issue Feb 20, 2023 · 1 comment

Comments

@DAreRodz
Copy link
Collaborator

Currently, the store state is initialized once (during hydration). However, when client-side navigation happens, it could contain newly defined state for the new HTML that should not be ignored.

We need to read and merge the new state into the current state, keeping the props it already has.

@luisherranz
Copy link
Member

To document here how to deal with this state merge:

  • If the state should be updated after client-side navigation, the developer won't have to do anything because it will be the default behavior.

    For example, imagine there is a state.myPlugin.pageLikes property with the number of likes of a certain page. That number will come populated from the server, and can be updated if the user clicks on the "Like" button. When the user navigates to another page, the state.myPlugin.pageLikes property must be replaced with the number of likes of the new page, populated again from the server.

  • If the state should not be updated after client-side navigation, the developer needs to create an additional property that is only present on the client (and therefore it won't be updated).

    For example, imagine a state.myPlugin.input property that populates an input and differs depending on each page. Users can edit the input; the input value should not change when they navigate to another page.

    In this case, the recommendation would be to rename the property to state.myPlugin.initialInput, and use it to populate a client-only state.myPlugin.input that won't be updated when both states are merged.

@WordPress WordPress locked and limited conversation to collaborators Jul 27, 2023
@luisherranz luisherranz converted this issue into a discussion Jul 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants