Portal 传送门
传送门组件将其子节点渲染到当前 DOM 结构之外的新 "子类树" 当中。
It looks like I will render here.
<button type="button" onClick={handleClick}>
{show ? 'Unmount children' : 'Mount children'}
</button>
<Box sx={{ p: 1, my: 1, border: '1px solid' }}>
It looks like I will render here.
{show ? (
<Portal container={container.current}>
<span>But I actually render here!</span>
</Portal>
) : null}
</Box>
<Box sx={{ p: 1, my: 1, border: '1px solid' }} ref={container} />
服务器端
React 不支持服务端渲染的 createPortal()
API。 您必须等到客户端的注水渲染(hydration)之后才能见到子节点。
Unstyled
As the component does not have any styles, it also comes with the unstyled package.
import Portal from '@material-ui/unstyled/Portal';