Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: return a NavigationContent component from useNavigationBuilder
- Loading branch information
Showing
14 changed files
with
246 additions
and
198 deletions.
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
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,30 @@ | ||
import * as React from 'react'; | ||
|
||
export default function useComponent< | ||
T extends React.ComponentType<any>, | ||
P extends {} | ||
>(Component: T, props: P) { | ||
const propsRef = React.useRef<P | null>(props); | ||
|
||
// Normally refs shouldn't be mutated in render | ||
// But we return a component which will be rendered | ||
// So it's just for immediate consumption | ||
propsRef.current = props; | ||
|
||
React.useEffect(() => { | ||
propsRef.current = null; | ||
}); | ||
|
||
return React.useRef((rest: Omit<React.ComponentProps<T>, keyof P>) => { | ||
const props = propsRef.current; | ||
|
||
if (props === null) { | ||
throw new Error( | ||
'The returned component must be rendered in the same render phase as the hook.' | ||
); | ||
} | ||
|
||
// @ts-expect-error: the props should be fine here | ||
return <Component {...props} {...rest} />; | ||
}).current; | ||
} |
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
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
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
Oops, something went wrong.