Skip to content

Commit

Permalink
fix: move consumer validation to avoid unnecessary warnings
Browse files Browse the repository at this point in the history
This commit moves the block that ensured the Provider was being used
inside the appropriate context into the component definition. This
removes the possibility of it logging a warning when ReactImgix
components are wrapped without the context component while also issuing
a warning when the provider is used without consumers.
  • Loading branch information
luqven committed Jun 17, 2021
1 parent cef074a commit b034d8a
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/HOCs/imgixProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ const ImgixContext = createContext()
* @returns Hook that gets context value from the nearest`ImgixProvider`.
*/
function useImgixContext() {
const context = useContext(ImgixContext)
if (context === undefined) {
console.error("useImgixContext must be used within a ImgixProvider")
}
return context
return useContext(ImgixContext)
}

/**
Expand All @@ -27,6 +23,9 @@ function useImgixContext() {
function ImgixProvider({children, ...reactImgixProps}) {
const value = reactImgixProps

if ( children == null || children.length < 1) {
console.error("ImgixProvider must have at least one Imgix child component")
}
return <ImgixContext.Provider value={value}>{children}</ImgixContext.Provider>
}

Expand Down

0 comments on commit b034d8a

Please sign in to comment.