From e0bda299c5b21f07bc0a01debe005892a88b1f84 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Mon, 2 Dec 2024 19:16:04 +0000 Subject: [PATCH 1/5] `getServerState()` & `getServerContext()` docs: Add note about reactivity. --- ...estanding-global-state-local-context-and-derived-state.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/reference-guides/interactivity-api/core-concepts/undestanding-global-state-local-context-and-derived-state.md b/docs/reference-guides/interactivity-api/core-concepts/undestanding-global-state-local-context-and-derived-state.md index f4fcbdf576c1cf..3ac3303f634991 100644 --- a/docs/reference-guides/interactivity-api/core-concepts/undestanding-global-state-local-context-and-derived-state.md +++ b/docs/reference-guides/interactivity-api/core-concepts/undestanding-global-state-local-context-and-derived-state.md @@ -745,6 +745,7 @@ When using region-based navigation, it's crucial to ensure that your interactive `getServerState()` allows you to subscribe to changes in the **global state** that occur during client-side navigation. This function is analogous to `getServerContext()`, but it works with the global state instead of the local context. +The `getServerState()` function returns a read-only reactive object. This means that any [callbacks](/docs/reference-guides/interactivity-api/api-reference.md#accessing-data-in-callbacks) you have defined that watch the returned object will only trigger when the value returned by the function changes. If the value remains the same, the callback will not re-trigger. Let's consider a quiz that has multiple questions. Each question is a separate page. When the user navigates to a new question, the server provides the new question and the time left to answer all the questions. ```php @@ -789,6 +790,8 @@ store( 'myPlugin', { `getServerContext()` allows you to subscribe to changes in the **local context** that occur during client-side navigation. This function is analogous to `getServerState()`, but it works with the local context instead of the global state. +The `getServerContext()` function returns a read-only reactive object. This means that any [callbacks](/docs/reference-guides/interactivity-api/api-reference.md#accessing-data-in-callbacks) you have defined that watch the returned object will only trigger when the value returned by the function changes. If the value remains the same, the callback will not re-trigger. + Consider a quiz that has multiple questions. Each question is a separate page. When the user navigates to a new question, the server provides the new question and the time left to answer all the questions. ```php @@ -835,7 +838,7 @@ Whenever you have interactive blocks that rely on global state that may change d ### Best Practices for using `getServerState()` and `getServerContext()` - **Read-Only References:** Both `getServerState()` and `getServerContext()` return read-only objects. You can use those objects to update the global state or local context. -- **Callback Integration:** Incorporate these functions within your store [callbacks](/docs/reference-guides/interactivity-api/api-reference.md#accessing-data-in-callbacks) to react to state and context changes. +- **Callback Integration:** Incorporate these functions within your store [callbacks](/docs/reference-guides/interactivity-api/api-reference.md#accessing-data-in-callbacks) to react to state and context changes. Both `getServerState()` and `getServerContext()` return reactive objects. This means that their watch callbacks will only trigger when the value of a property changes. If the value remains the same, the callback will not re-trigger. ## Conclusion From 3033346598b8b570dbddb1877c749a9fdf4b6ac1 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Mon, 2 Dec 2024 19:16:25 +0000 Subject: [PATCH 2/5] Fix the code snippets --- ...-global-state-local-context-and-derived-state.md | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/docs/reference-guides/interactivity-api/core-concepts/undestanding-global-state-local-context-and-derived-state.md b/docs/reference-guides/interactivity-api/core-concepts/undestanding-global-state-local-context-and-derived-state.md index 3ac3303f634991..7f7a3af78b2ac3 100644 --- a/docs/reference-guides/interactivity-api/core-concepts/undestanding-global-state-local-context-and-derived-state.md +++ b/docs/reference-guides/interactivity-api/core-concepts/undestanding-global-state-local-context-and-derived-state.md @@ -746,15 +746,14 @@ When using region-based navigation, it's crucial to ensure that your interactive `getServerState()` allows you to subscribe to changes in the **global state** that occur during client-side navigation. This function is analogous to `getServerContext()`, but it works with the global state instead of the local context. The `getServerState()` function returns a read-only reactive object. This means that any [callbacks](/docs/reference-guides/interactivity-api/api-reference.md#accessing-data-in-callbacks) you have defined that watch the returned object will only trigger when the value returned by the function changes. If the value remains the same, the callback will not re-trigger. + Let's consider a quiz that has multiple questions. Each question is a separate page. When the user navigates to a new question, the server provides the new question and the time left to answer all the questions. ```php - get_question_for_page( get_the_ID() ), 'timeLeft' => 5 * 60, // Time to answer all the questions. -) ); -?> +) ); ?>> ``` ```javascript @@ -795,11 +794,9 @@ The `getServerContext()` function returns a read-only reactive object. This mean Consider a quiz that has multiple questions. Each question is a separate page. When the user navigates to a new question, the server provides the new question and the time left to answer all the questions. ```php - get_question_for_page( get_the_ID() ), -) ); -?> +) ); ?>> ``` ```javascript From d4f57f4715c7bc5349004d2e7c20cb9a9186d67d Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Mon, 2 Dec 2024 19:17:23 +0000 Subject: [PATCH 3/5] Add note about local context --- ...undestanding-global-state-local-context-and-derived-state.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/reference-guides/interactivity-api/core-concepts/undestanding-global-state-local-context-and-derived-state.md b/docs/reference-guides/interactivity-api/core-concepts/undestanding-global-state-local-context-and-derived-state.md index 7f7a3af78b2ac3..3ac88fc6af69f8 100644 --- a/docs/reference-guides/interactivity-api/core-concepts/undestanding-global-state-local-context-and-derived-state.md +++ b/docs/reference-guides/interactivity-api/core-concepts/undestanding-global-state-local-context-and-derived-state.md @@ -830,7 +830,7 @@ store( 'myPlugin', { ### When to Use -Whenever you have interactive blocks that rely on global state that may change due to navigation events, ensuring consistency across different parts of your application. +Whenever you have interactive blocks that rely on global state or local context that may change due to navigation events, ensuring consistency across different parts of your application. ### Best Practices for using `getServerState()` and `getServerContext()` From 377d59cb4bef0901aecfd3b56eae55a77e3ddbc6 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Mon, 2 Dec 2024 20:36:09 +0000 Subject: [PATCH 4/5] Fix param order and function name --- ...anding-global-state-local-context-and-derived-state.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/reference-guides/interactivity-api/core-concepts/undestanding-global-state-local-context-and-derived-state.md b/docs/reference-guides/interactivity-api/core-concepts/undestanding-global-state-local-context-and-derived-state.md index 3ac88fc6af69f8..9b69f691a3ecd3 100644 --- a/docs/reference-guides/interactivity-api/core-concepts/undestanding-global-state-local-context-and-derived-state.md +++ b/docs/reference-guides/interactivity-api/core-concepts/undestanding-global-state-local-context-and-derived-state.md @@ -750,10 +750,10 @@ The `getServerState()` function returns a read-only reactive object. This means Let's consider a quiz that has multiple questions. Each question is a separate page. When the user navigates to a new question, the server provides the new question and the time left to answer all the questions. ```php -
get_question_for_page( get_the_ID() ), 'timeLeft' => 5 * 60, // Time to answer all the questions. -) ); ?>> +), 'myPlugin' ); ?>> ``` ```javascript @@ -794,9 +794,9 @@ The `getServerContext()` function returns a read-only reactive object. This mean Consider a quiz that has multiple questions. Each question is a separate page. When the user navigates to a new question, the server provides the new question and the time left to answer all the questions. ```php -
get_question_for_page( get_the_ID() ), -) ); ?>> +), 'myPlugin' ); ?>> ``` ```javascript From c78601fb7d1633a10cbbc6d5ddded607acb2d7bf Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Tue, 3 Dec 2024 11:39:21 +0000 Subject: [PATCH 5/5] Remove the namespace and use the correct functions --- ...standing-global-state-local-context-and-derived-state.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/reference-guides/interactivity-api/core-concepts/undestanding-global-state-local-context-and-derived-state.md b/docs/reference-guides/interactivity-api/core-concepts/undestanding-global-state-local-context-and-derived-state.md index 9b69f691a3ecd3..f6145749a00b19 100644 --- a/docs/reference-guides/interactivity-api/core-concepts/undestanding-global-state-local-context-and-derived-state.md +++ b/docs/reference-guides/interactivity-api/core-concepts/undestanding-global-state-local-context-and-derived-state.md @@ -750,10 +750,10 @@ The `getServerState()` function returns a read-only reactive object. This means Let's consider a quiz that has multiple questions. Each question is a separate page. When the user navigates to a new question, the server provides the new question and the time left to answer all the questions. ```php -
get_question_for_page( get_the_ID() ), 'timeLeft' => 5 * 60, // Time to answer all the questions. -), 'myPlugin' ); ?>> +) ); ?>> ``` ```javascript @@ -796,7 +796,7 @@ Consider a quiz that has multiple questions. Each question is a separate page. W ```php
get_question_for_page( get_the_ID() ), -), 'myPlugin' ); ?>> +), ); ?>> ``` ```javascript