@@ -20,7 +20,7 @@ const useEnhancedEffect = typeof window !== 'undefined' ? React.useLayoutEffect
2020const Portal = React . forwardRef ( function Portal ( props , ref ) {
2121 const { children, container, disablePortal = false , onRendered } = props ;
2222 const [ mountNode , setMountNode ] = React . useState ( null ) ;
23- const handleRef = useForkRef ( children . ref , ref ) ;
23+ const handleRef = useForkRef ( React . isValidElement ( children ) ? children . ref : null , ref ) ;
2424
2525 useEnhancedEffect ( ( ) => {
2626 if ( ! disablePortal ) {
@@ -46,10 +46,12 @@ const Portal = React.forwardRef(function Portal(props, ref) {
4646 } , [ onRendered , mountNode , disablePortal ] ) ;
4747
4848 if ( disablePortal ) {
49- React . Children . only ( children ) ;
50- return React . cloneElement ( children , {
51- ref : handleRef ,
52- } ) ;
49+ if ( React . isValidElement ( children ) ) {
50+ return React . cloneElement ( children , {
51+ ref : handleRef ,
52+ } ) ;
53+ }
54+ return children ;
5355 }
5456
5557 return mountNode ? ReactDOM . createPortal ( children , mountNode ) : mountNode ;
@@ -63,7 +65,7 @@ Portal.propTypes = {
6365 /**
6466 * The children to render into the `container`.
6567 */
66- children : PropTypes . node . isRequired ,
68+ children : PropTypes . node ,
6769 /**
6870 * A node, component instance, or function that returns either.
6971 * The `container` will have the portal children appended to it.
0 commit comments