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

Fix missing context error #51

Merged
merged 1 commit into from
Aug 8, 2022
Merged

Conversation

ockham
Copy link
Collaborator

@ockham ockham commented Aug 8, 2022

Fix #46, according to my comment:

I think it would make sense if "our" useContext was consistent with React's and -- in the absence of a Provider -- returned the default value used when creating the context:

const MyContext = React.createContext(defaultValue);
[...]
The defaultValue argument is only used when a component does not have a matching Provider above it in the tree. This default value can be helpful for testing components in isolation without wrapping them. Note: passing undefined as a Provider value does not cause consuming components to use defaultValue.

(source)

Testing instructions

  • Verify that an Interactive Child block inside a Non-interactive Parent block works now:

image

  • Verify that the Interactive Child block falls back to the default context passed when creating the context by applying the following patch:
diff --git a/src/context/counter.js b/src/context/counter.js
index 6061f47..0fa0325 100644
--- a/src/context/counter.js
+++ b/src/context/counter.js
@@ -1,7 +1,7 @@
 import { createContext } from '@wordpress/element';
 
 if (typeof window.reactContext === 'undefined') {
-       window.reactContext = createContext(null);
+       window.reactContext = createContext(0);
 }
 window.reactContext.displayName = 'CounterContext';
 export default window.reactContext;
diff --git a/src/context/theme.js b/src/context/theme.js
index 8aefc8e..d63cc62 100644
--- a/src/context/theme.js
+++ b/src/context/theme.js
@@ -1,7 +1,7 @@
 import { createContext } from '@wordpress/element';
 
 if (typeof window.themeReactContext === 'undefined') {
-       window.themeReactContext = createContext(null);
+       window.themeReactContext = createContext('twentytwentytwo');
 }
 window.themeReactContext.displayName = 'ThemeContext';
 export default window.themeReactContext;

Result:

image

  • Verify that the if there is an intermediate layer with an Interactive Parent block, its context continues to be respected by the child block:

image

Comment on lines -113 to +115
Providers.push(event.detail.Provider);
if (typeof event.detail.Provider === 'function') {
Providers.push(event.detail.Provider);
}
Copy link
Collaborator Author

@ockham ockham Aug 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem was that event.detail.Provider is undefined if no Provider is... provided 🙃 , and that we're expecting a function that we later call.

If we only add Provider if it is a function, we fix the bug and get the desired behavior (falling back to the argument passed to createContext) for free 🎉

@ockham ockham self-assigned this Aug 8, 2022
@ockham ockham requested a review from a team August 8, 2022 15:57
@ockham ockham marked this pull request as ready for review August 8, 2022 15:58
Copy link
Collaborator

@cbravobernal cbravobernal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@cbravobernal cbravobernal merged commit 2a15bce into main Aug 8, 2022
@cbravobernal cbravobernal deleted the fix/missing-context-error branch August 8, 2022 16:59
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Child using React context throws when there is no parent that provides that context
2 participants