Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slot/Fill: Error when provider is missing #23388

Closed
sirreal opened this issue Jun 23, 2020 · 0 comments · Fixed by #23493
Closed

Slot/Fill: Error when provider is missing #23388

sirreal opened this issue Jun 23, 2020 · 0 comments · Fixed by #23493
Labels
Needs Dev Ready for, and needs developer efforts [Package] Components /packages/components [Type] Code Quality Issues or PRs that relate to code quality

Comments

@sirreal
Copy link
Member

sirreal commented Jun 23, 2020

The design of Slot/Fill context hides problematic cases where a Consumer is not mounted under the necessary Provider.

It would be preferable to remove the stubbed context that hides misuse in applications. I'd like to see a descriptive error thrown immediately when the context is used without a provider:

/**
* WordPress dependencies
*/
import { createContext } from '@wordpress/element';
const SlotFillContext = createContext( {
slots: {},
fills: {},
registerSlot: () => {},
updateSlot: () => {},
unregisterSlot: () => {},
registerFill: () => {},
unregisterFill: () => {},
} );
export default SlotFillContext;

An error in useSlot seems ideal:

const registry = useContext( SlotFillContext );

const SlotFillContext = createContext( undefined );
function useSlot( name ) {
  const registry = useContext( SlotFillContext );
  if ( ! registry ) {
    throw new Error( 'useSlot must be a descendent of SlotFillProvider' );
  }
  // …
}

#23014 describes a bug where SlotFillProvider is not an ancestor of a SlotFillContext Consumer.

#23108 fixes the error, but the caveats were noted in several places.

cc: @gziolo @youknowriad @diegohaz

@sirreal sirreal added [Type] Code Quality Issues or PRs that relate to code quality [Package] Components /packages/components labels Jun 23, 2020
@sirreal sirreal changed the title Slot/Fill: Warn or error when provider is missing Slot/Fill: Error when provider is missing Jun 23, 2020
@gziolo gziolo added the Needs Dev Ready for, and needs developer efforts label Jun 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Dev Ready for, and needs developer efforts [Package] Components /packages/components [Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants