-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
safely validate root prop for SSR #5
- Loading branch information
1 parent
9c0f178
commit 4ede0c4
Showing
2 changed files
with
26 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// SSR safe Valid HTML Element prop check | ||
|
||
export default function validHTMLElement( | ||
props, | ||
propName, | ||
componentName = 'ANONYMOUS' | ||
) { | ||
if (typeof window === 'undefined') { | ||
return null; | ||
} | ||
|
||
if (props[propName]) { | ||
const value = props[propName]; | ||
const isValid = value instanceof window.Element; | ||
|
||
if (!isValid) { | ||
return new Error( | ||
`Prop name "${propName}" in <${componentName}> must be an HTML DOM element.` | ||
); | ||
} | ||
} | ||
|
||
return null; | ||
} |