Skip to content

Instantly share code, notes, and snippets.

@Akiyamka
Last active November 13, 2019 14:20
Show Gist options
  • Save Akiyamka/5269d67f8a5a3796debc436703195417 to your computer and use it in GitHub Desktop.
Save Akiyamka/5269d67f8a5a3796debc436703195417 to your computer and use it in GitHub Desktop.
React portal in real life
import React, { useRef } from 'react';
import ReactDOM from 'react-dom';
const Portal = {
open(ref) {
this.ref = ref;
},
close() {
this.ref = null;
},
component({ children }) {
return this.ref ? ReactDOM.createPortal(
children,
this.ref
) : null;
}
};
export default function Tooltips() {
const tooltipRef = useRef();
tooltipRef.current && Portal.open(tooltipRef.current);
return <div ref={tooltipRef}>
{/* Some layout */}
</div>
}
export function PortalToTooltips(props) {
return Portal.component(props);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment