React如何將組件渲染到指定DOM節(jié)點(diǎn)詳解
前言
眾所周知React優(yōu)點(diǎn)之一就是他的API特別簡單。通過render 方法返回一個組件的基本結(jié)構(gòu),如同一個簡單的函數(shù),就可以得到一個可以復(fù)用的react組件。但是有時候還是會有些限制的,尤其是他的API中,不能控制組件所應(yīng)該渲染到的DOM節(jié)點(diǎn),這就讓一些彈層組件很難控制。當(dāng)父元素設(shè)置為overflow:hidden 的時候,問題就會出現(xiàn)了。
例如就像下面的這樣:

我們實(shí)際期待的效果是這樣的:

幸運(yùn)的是,雖然不是很明顯,但有一個相當(dāng)優(yōu)雅的方式來繞過這個問題。我們學(xué)到的第一個react函數(shù)是render 方法,他的函數(shù)簽名是這樣的:
ReactComponent render( ReactElement element, DOMElement container, [function callback] )
通常情況下我們使用該方法將整個應(yīng)用渲染到一個DOM節(jié)點(diǎn)中。好消息是該方法并不僅僅局限于此。我們可以在一個組件中,使用ReactDom.render 方法將另一個組件渲染到一個指定的DOM 元素中。作為一個組件的render 方法,其必須是純凈的(例如:不能改變state或者與DOM交互).所以我們需要在componentDidUpdate 或者 componentDidMount 中調(diào)用ReactDom.render 方法。
另外我們需要確保在父元素被卸載的時候,改組件也要被卸載掉.
整理下,我們得到下面的一個組件:
import React,{Component} from 'react';
import ReactDom from 'react-dom';
export default class RenderInBody extends Component{
constructor(p){
super();
}
componentDidMount(){//新建一個div標(biāo)簽并塞進(jìn)body
this.popup = document.createElement("div");
document.body.appendChild(this.popup);
this._renderLayer();
}
componentDidUpdate() {
this._renderLayer();
}
componentWillUnmount(){//在組件卸載的時候,保證彈層也被卸載掉
ReactDom.unmountComponentAtNode(this.popup);
document.body.removeChild(this.popup);
}
_renderLayer(){//將彈層渲染到body下的div標(biāo)簽
ReactDom.render(this.props.children, this.popup);
}
render(){
return null;
}
}
總結(jié)下就是:
在componentDidMount的時候手動向body內(nèi)塞一個div標(biāo)簽,然后使用ReactDom.render 將組件渲染到這個div標(biāo)簽
當(dāng)我們想把組件直接渲染到body上的時候,只需要在該組件的外面包一層RenderInBody 就可以了.
export default class Dialog extends Component{
render(){
return {
<RenderInBody>i am a dialog render to body</RenderInBody>
}
}
}
譯者增加:
將以上組件改造一下,我們就可以向指定的dom節(jié)點(diǎn)中渲染和卸載組件,并加上位置控制,如下:
//此組件用于在body內(nèi)渲染彈層
import React,{Component} from 'react'
import ReactDom from 'react-dom';
export default class RenderInBody extends Component{
constructor(p){
super(p);
}
componentDidMount(){
/**
popupInfo={
rootDom:***,//接收彈層組件的DOM節(jié)點(diǎn),如document.body
left:***,//相對位置
top:***//位置信息
}
*/
let {popupInfo} = this.props;
this.popup = document.createElement('div');
this.rootDom = popupInfo.rootDom;
this.rootDom.appendChild(this.popup);
//we can setAttribute of the div only in this way
this.popup.style.position='absolute';
this.popup.style.left=popupInfo.left+'px';
this.popup.style.top=popupInfo.top+'px';
this._renderLayer()
}
componentDidUpdate() {
this._renderLayer();
}
componentWillUnmount(){
this.rootDom.removeChild(this.popup);
}
_renderLayer(){
ReactDom.render(this.props.children, this.popup);
}
render(){
return null;
}
}
注:位置獲取和根結(jié)點(diǎn)判斷函數(shù)
export default (dom,classFilters)=> {
let left = dom.offsetLeft,
top = dom.offsetTop + dom.scrollTop,
current = dom.offsetParent,
rootDom = accessBodyElement(dom);//默認(rèn)是body
while (current !=null ) {
left += current.offsetLeft;
top += current.offsetTop;
current = current.offsetParent;
if (current && current.matches(classFilters)) {
rootDom = current;
break;
}
}
return { left: left, top: top ,rootDom:rootDom};
}
/***
1. dom:為響應(yīng)彈層的dom節(jié)點(diǎn),或者到該dom的位置后,可以做位置的微調(diào),讓彈層位置更佳合適
*
2. classFilters:需要接收彈層組件的DOM節(jié)點(diǎn)的篩選類名
/
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
關(guān)于react hook useState連續(xù)更新對象的問題
這篇文章主要介紹了關(guān)于react hook useState連續(xù)更新對象的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
淺談React的React.FC與React.Component的使用
本文主要介紹了React的React.FC與React.Component的使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
深入理解React Native核心原理(React Native的橋接(Bridge)
這篇文章主要介紹了深入理解React Native核心原理(React Native的橋接(Bridge),本文重點(diǎn)給大家介紹React Native的基礎(chǔ)知識及實(shí)現(xiàn)原理,需要的朋友可以參考下2021-04-04
react?diff?算法實(shí)現(xiàn)思路及原理解析
這篇文章主要介紹了react?diff?算法實(shí)現(xiàn)思路及原理解析,本節(jié)我們正式進(jìn)入基本面試必考的核心地帶?--?diff?算法,了解如何優(yōu)化和復(fù)用?dom?操作的,還有我們常見的?key?的作用,需要的朋友可以參考下2022-05-05

