Merge new SSR state on client-side navigation #53056
Replies: 4 comments 4 replies
-
To document here how to deal with this state merge:
|
Beta Was this translation helpful? Give feedback.
-
This is a pattern that people could use when they want to ignore the new server value of some state property and preserve the one that is in the client, but make it transparent for the wp_initial_state([
"initialNumberOfItems" => 3
]); const { state } = store("myPlugin", {
state: {
currentNumberOfItems: null,
get numberOfItems() {
return state.currentNumberOfItems ?? state.initialNumberOfItems;
},
set numberOfItems(value) {
state.currentNumberOfItems = value;
},
},
action: {
addOneItem: () => {
// Consumers/mutators can always use `state.numberOfItems`
state.numberOfItems += 1;
},
},
}); |
Beta Was this translation helpful? Give feedback.
-
That is pretty cool, thanks! I am curious about loading/merging the required styles from blocks used on other pages, when using client-side navigation. Is that possible? Im fetching the page content of a client-side navigated page and displaying the rendered content, which happens to be woocommerce products. Some of the block styles come through, but not all of them. If I refresh the page, then all the required styles come through. |
Beta Was this translation helpful? Give feedback.
-
I'm closing this discussion as the SSR state merge was solved in the following PR: |
Beta Was this translation helpful? Give feedback.
-
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.
Beta Was this translation helpful? Give feedback.
All reactions