Posted code绉樺瘑鑺卞洯
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。
瀵艰
React
鐨勮櫄鎷?DOM
鍜?Diff
绠楁硶鏄?React
鐨勯潪甯搁噸瑕佺殑鏍稿績鐗规€э紝杩欓儴鍒嗘簮鐮佷篃闈炲父澶嶆潅锛岀悊瑙h繖閮ㄥ垎鐭ヨ瘑鐨勫師鐞嗗鏇存繁鍏ョ殑鎺屾彙 React
鏄潪甯稿繀瑕佺殑銆?/p>
鏈潵鎯冲皢铏氭嫙 DOM
鍜?Diff
绠楁硶鏀惧埌涓€绡囨枃绔狅紝鍐欏畬铏氭嫙 DOM
鍙戠幇鏂囩珷宸茬粡寰堥暱浜嗭紝鎵€浠ユ湰绡囧彧鍒嗘瀽铏氭嫙 DOM
銆?/p>
鏈瘒鏂囩珷浠庢簮鐮佸嚭鍙戯紝鍒嗘瀽铏氭嫙 DOM
鐨勬牳蹇冩覆鏌撳師鐞嗭紝浠ュ強 React
瀵瑰畠鍋氱殑鎬ц兘浼樺寲鐐广€?/p>
璇村疄璇?React
婧愮爜鐪熺殑寰堥毦璇火煒咃紝濡傛灉鏈瘒鏂囩珷甯姪鍒颁簡浣狅紝閭d箞璇风粰涓禐馃憤鏀寔涓€涓嬪惂銆?/p>
鏈枃鐣ラ暱锛屼笅闈㈡槸鏈枃鐨勬€濈淮瀵煎浘锛屾偍鍙互浠庡ご寮€濮嬮槄璇伙紝涔熷彲浠ラ€夋嫨鎰熷叴瓒g殑閮ㄥ垎闃呰锛?/p>
寮€鍙戜腑鐨勫父瑙侀棶棰?/h2>
涓轰綍蹇呴』寮曠敤 React
鑷畾涔夌殑 React
缁勪欢涓轰綍蹇呴』澶у啓
React
濡備綍闃叉 XSS
React
鐨?nbsp;Diff
绠楁硶鍜屽叾浠栫殑 Diff
绠楁硶鏈変綍鍖哄埆
key
鍦?nbsp;React
涓殑浣滅敤
濡備綍鍐欏嚭楂樻€ц兘鐨?nbsp;React
缁勪欢
涓轰綍蹇呴』寮曠敤 React
鑷畾涔夌殑 React
缁勪欢涓轰綍蹇呴』澶у啓
React
濡備綍闃叉 XSS
React
鐨?nbsp;Diff
绠楁硶鍜屽叾浠栫殑 Diff
绠楁硶鏈変綍鍖哄埆
key
鍦?nbsp;React
涓殑浣滅敤
濡備綍鍐欏嚭楂樻€ц兘鐨?nbsp;React
缁勪欢
濡傛灉浣犲涓婇潰鍑犱釜闂杩樺瓨鍦ㄧ枒闂紝璇存槑浣犲 React
鐨勮櫄鎷?DOM
浠ュ強 Diff
绠楁硶瀹炵幇鍘熺悊杩樻湁鎵€娆犵己锛岄偅涔堣濂藉ソ闃呰鏈瘒鏂囩珷鍚с€?/p>
棣栧厛鎴戜滑鏉ョ湅鐪嬪埌搴曚粈涔堟槸铏氭嫙 DOM
:
铏氭嫙DOM
鍦ㄥ師鐢熺殑 javascript
绋嬪簭涓紝鎴戜滑鐩存帴瀵?DOM
杩涜鍒涘缓鍜屾洿鏀癸紝鑰?DOM
鍏冪礌閫氳繃鎴戜滑鐩戝惉鐨勪簨浠跺拰鎴戜滑鐨勫簲鐢ㄧ▼搴忚繘琛岄€氳銆?/p>
鑰?React
浼氬厛灏嗕綘鐨勪唬鐮佽浆鎹㈡垚涓€涓?JavaScript
瀵硅薄锛岀劧鍚庤繖涓?JavaScript
瀵硅薄鍐嶈浆鎹㈡垚鐪熷疄 DOM
銆傝繖涓?nbsp;JavaScript
瀵硅薄灏辨槸鎵€璋撶殑铏氭嫙 DOM
銆?/p>
姣斿涓嬮潰涓€娈?html
浠g爜锛?/p>
<div class="title">
<span>Hello ConardLi</span>
<ul>
<li>鑻规灉</li>
<li>姗樺瓙</li>
</ul>
</div>
鍦?React
鍙兘瀛樺偍涓鸿繖鏍风殑 JS
浠g爜锛?/p>
const VitrualDom = {
type: 'div',
props: { class: 'title' },
children: [
{
type: 'span',
children: 'Hello ConardLi'
},
{
type: 'ul',
children: [
{ type: 'ul', children: '鑻规灉' },
{ type: 'ul', children: '姗樺瓙' }
]
}
]
}
褰撴垜浠渶瑕佸垱寤烘垨鏇存柊鍏冪礌鏃讹紝 React
棣栧厛浼氳杩欎釜 VitrualDom
瀵硅薄杩涜鍒涘缓鍜屾洿鏀癸紝鐒跺悗鍐嶅皢 VitrualDom
瀵硅薄娓叉煋鎴愮湡瀹?DOM
锛?/p>
褰撴垜浠渶瑕佸 DOM
杩涜浜嬩欢鐩戝惉鏃讹紝棣栧厛瀵?VitrualDom
杩涜浜嬩欢鐩戝惉锛?VitrualDom
浼氫唬鐞嗗師鐢熺殑 DOM
浜嬩欢浠庤€屽仛鍑哄搷搴斻€?/p>
涓轰綍浣跨敤铏氭嫙DOM
React
涓轰綍閲囩敤 VitrualDom
杩欑鏂规鍛紵
鎻愰珮寮€鍙戞晥鐜?/h3>
浣跨敤 JavaScript
锛屾垜浠湪缂栧啓搴旂敤绋嬪簭鏃剁殑鍏虫敞鐐瑰湪浜庡浣曟洿鏂?DOM
銆?/p>
浣跨敤 React
锛屼綘鍙渶瑕佸憡璇?React
浣犳兂璁╄鍥惧浜庝粈涔堢姸鎬侊紝 React
鍒欓€氳繃 VitrualDom
纭繚 DOM
涓庤鐘舵€佺浉鍖归厤銆備綘涓嶅繀鑷繁鍘诲畬鎴愬睘鎬ф搷浣溿€佷簨浠跺鐞嗐€?nbsp;DOM
鏇存柊锛?React
浼氭浛浣犲畬鎴愯繖涓€鍒囥€?/p>
鍏充簬鎻愬崌鎬ц兘
寰堝鏂囩珷璇?VitrualDom
鍙互鎻愬崌鎬ц兘锛岃繖涓€璇存硶瀹為檯涓婃槸寰堢墖闈㈢殑銆?/p>
鐩存帴鎿嶄綔 DOM
鏄潪甯歌€楄垂鎬ц兘鐨勶紝杩欎竴鐐规瘚搴哥疆鐤戙€備絾鏄?nbsp;React
浣跨敤 VitrualDom
涔熸槸鏃犳硶閬垮厤鎿嶄綔 DOM
鐨勩€?/p>
濡傛灉鏄娆℃覆鏌擄紝 VitrualDom
涓嶅叿鏈変换浣曚紭鍔匡紝鐢氳嚦瀹冭杩涜鏇村鐨勮绠楋紝娑堣€楁洿澶氱殑鍐呭瓨銆?/p>
VitrualDom
鐨勪紭鍔垮湪浜?React
鐨?Diff
绠楁硶鍜屾壒澶勭悊绛栫暐锛?React
鍦ㄩ〉闈㈡洿鏂颁箣鍓嶏紝鎻愬墠璁$畻濂戒簡濡備綍杩涜鏇存柊鍜屾覆鏌?DOM
銆傚疄闄呬笂锛岃繖涓绠楄繃绋嬫垜浠湪鐩存帴鎿嶄綔 DOM
鏃讹紝涔熸槸鍙互鑷繁鍒ゆ柇鍜屽疄鐜扮殑锛屼絾鏄竴瀹氫細鑰楄垂闈炲父澶氱殑绮惧姏鍜屾椂闂达紝鑰屼笖寰€寰€鎴戜滑鑷繁鍋氱殑鏄笉濡?React
濂界殑銆傛墍浠ワ紝鍦ㄨ繖涓繃绋嬩腑 React
甯姪鎴戜滑"鎻愬崌浜嗘€ц兘"銆?/p>
鎵€浠ワ紝鎴戞洿鍊惧悜浜庤锛?VitrualDom
甯姪鎴戜滑鎻愰珮浜嗗紑鍙戞晥鐜囷紝鍦ㄩ噸澶嶆覆鏌撴椂瀹冨府鍔╂垜浠绠楀浣曟洿楂樻晥鐨勬洿鏂帮紝鑰屼笉鏄畠姣?DOM
鎿嶄綔鏇村揩銆?/p>
濡傛灉鎮ㄥ鏈儴鍒嗙殑鍒嗘瀽鏈変粈涔堜笉鍚岃瑙o紝娆㈣繋鍦ㄨ瘎璁哄尯鎷嶇爾銆?/p>
璺ㄦ祻瑙堝櫒鍏煎
React
鍩轰簬 VitrualDom
鑷繁瀹炵幇浜嗕竴濂楄嚜宸辩殑浜嬩欢鏈哄埗锛岃嚜宸辨ā鎷熶簡浜嬩欢鍐掓场鍜屾崟鑾风殑杩囩▼锛岄噰鐢ㄤ簡浜嬩欢浠g悊锛屾壒閲忔洿鏂扮瓑鏂规硶锛屾姽骞充簡鍚勪釜娴忚鍣ㄧ殑浜嬩欢鍏煎鎬ч棶棰樸€?/p>
璺ㄥ钩鍙板吋瀹?/h3>
VitrualDom
涓?React
甯︽潵浜嗚法骞冲彴娓叉煋鐨勮兘鍔涖€備互 ReactNative
涓轰緥瀛愩€?nbsp;React
鏍规嵁 VitrualDom
鐢诲嚭鐩稿簲骞冲彴鐨?ui
灞傦紝鍙笉杩囦笉鍚屽钩鍙扮敾鐨勫Э鍔夸笉鍚岃€屽凡銆?/p>
铏氭嫙DOM瀹炵幇鍘熺悊
濡傛灉浣犱笉鎯崇湅绻佹潅鐨勬簮鐮侊紝鎴栬€呯幇鍦ㄦ病鏈夎冻澶熸椂闂达紝鍙互璺宠繃杩欎竴绔狅紝鐩存帴馃憞铏氭嫙DOM鍘熺悊鎬荤粨
鍦ㄤ笂闈㈢殑鍥句笂鎴戜滑缁х画杩涜鎵╁睍锛屾寜鐓у浘涓殑娴佺▼锛屾垜浠緷娆℃潵鍒嗘瀽铏氭嫙 DOM
鐨勫疄鐜板師鐞嗐€?/p>
JSX鍜宑reateElement
鎴戜滑鍦ㄥ疄鐜颁竴涓?React
缁勪欢鏃跺彲浠ラ€夋嫨涓ょ缂栫爜鏂瑰紡锛岀涓€绉嶆槸浣跨敤 JSX
缂栧啓锛?/p>
class Hello extends Component {
render() {
return <div>Hello ConardLi</div>;
}
}
绗簩绉嶆槸鐩存帴浣跨敤 React.createElement
缂栧啓锛?/p>
class Hello extends Component {
render() {
return React.createElement('div', null, `Hello ConardLi`);
}
}
瀹為檯涓婏紝涓婇潰涓ょ鍐欐硶鏄瓑浠风殑锛?JSX
鍙槸涓?React.createElement(component,props,...children)
鏂规硶鎻愪緵鐨勮娉曠硸銆備篃灏辨槸璇存墍鏈夌殑 JSX
浠g爜鏈€鍚庨兘浼氳浆鎹㈡垚 React.createElement(...)
锛?Babel
甯姪鎴戜滑瀹屾垚浜嗚繖涓浆鎹㈢殑杩囩▼銆?/p>
濡備笅闈㈢殑 JSX
<div>
<img src="avatar.png" className="profile" />
<Hello />
</div>;
灏嗕細琚?Babel
杞崲涓?/p>
React.createElement("div", null, React.createElement("img", {
src: "avatar.png",
className: "profile"
}), React.createElement(Hello, null));
娉ㄦ剰锛?babel
鍦ㄧ紪璇戞椂浼氬垽鏂?JSX
涓粍浠剁殑棣栧瓧姣嶏紝褰撻瀛楁瘝涓哄皬鍐欐椂锛屽叾琚瀹氫负鍘熺敓 DOM
鏍囩锛?createElement
鐨勭涓€涓彉閲忚缂栬瘧涓哄瓧绗︿覆锛涘綋棣栧瓧姣嶄负澶у啓鏃讹紝鍏惰璁ゅ畾涓鸿嚜瀹氫箟缁勪欢锛?nbsp;createElement
鐨勭涓€涓彉閲忚缂栬瘧涓哄璞★紱
鍙﹀锛岀敱浜?JSX
鎻愬墠瑕佽 Babel
缂栬瘧锛屾墍浠?JSX
鏄笉鑳藉湪杩愯鏃跺姩鎬侀€夋嫨绫诲瀷鐨勶紝姣斿涓嬮潰鐨勪唬鐮侊細
function Story(props) {
// Wrong! JSX type can't be an expression.
return <components[props.storyType] story={props.story} />;
}
闇€瑕佸彉鎴愪笅闈㈢殑鍐欐硶锛?/p>
function Story(props) {
// Correct! JSX type can be a capitalized variable.
const SpecificStory = components[props.storyType];
return <SpecificStory story={props.story} />;
}
鎵€浠ワ紝浣跨敤 JSX
浣犻渶瑕佸畨瑁?Babel
鎻掍欢 babel-plugin-transform-react-jsx
{
"plugins": [
["transform-react-jsx", {
"pragma": "React.createElement"
}]
]
}
鍒涘缓铏氭嫙DOM
涓嬮潰鎴戜滑鏉ョ湅鐪嬭櫄鎷?DOM
鐨勭湡瀹炴ā鏍凤紝灏嗕笅闈㈢殑 JSX
浠g爜鍦ㄦ帶鍒跺彴鎵撳嵃鍑烘潵锛?/p>
<div className="title">
<span>Hello ConardLi</span>
<ul>
<li>鑻规灉</li>
<li>姗樺瓙</li>
</ul>
</div>
杩欎釜缁撴瀯鍜屾垜浠笂闈㈣嚜宸辨弿缁樼殑缁撴瀯寰堝儚锛岄偅涔?React
鏄浣曞皢鎴戜滑鐨勪唬鐮佽浆鎹㈡垚杩欎釜缁撴瀯鐨勫憿锛屼笅闈㈡垜浠潵鐪嬬湅 createElement
鍑芥暟鐨勫叿浣撳疄鐜帮紙鏂囦腑鐨勬簮鐮佺粡杩囩簿绠€锛夈€?/p>
createElement
鍑芥暟鍐呴儴鍋氱殑鎿嶄綔寰堢畝鍗曪紝灏?props
鍜屽瓙鍏冪礌杩涜澶勭悊鍚庤繑鍥炰竴涓?ReactElement
瀵硅薄锛屼笅闈㈡垜浠潵閫愪竴鍒嗘瀽锛?/p>
(1).澶勭悊props锛?/strong>
1.灏嗙壒娈婂睘鎬?nbsp;ref
銆?nbsp;key
浠?nbsp;config
涓彇鍑哄苟璧嬪€?/span>
2.灏嗙壒娈婂睘鎬?nbsp;self
銆?nbsp;source
浠?nbsp;config
涓彇鍑哄苟璧嬪€?/span>
3.灏嗛櫎鐗规畩灞炴€х殑鍏朵粬灞炴€у彇鍑哄苟璧嬪€肩粰 props
鍚庨潰鐨勬枃绔犱細璇︾粏浠嬬粛杩欎簺鐗规畩灞炴€х殑浣滅敤銆?/p>
(2).鑾峰彇瀛愬厓绱?/strong>
1.鑾峰彇瀛愬厓绱犵殑涓暟 鈥斺€?绗簩涓弬鏁板悗闈㈢殑鎵€鏈夊弬鏁?/span>
2.鑻ュ彧鏈変竴涓瓙鍏冪礌锛岃祴鍊肩粰 props.children
3.鑻ユ湁澶氫釜瀛愬厓绱狅紝灏嗗瓙鍏冪礌濉厖涓轰竴涓暟缁勮祴鍊肩粰 props.children
(3).澶勭悊榛樿props
灏嗙粍浠剁殑闈欐€佸睘鎬?nbsp;defaultProps
瀹氫箟鐨勯粯璁?nbsp;props
杩涜璧嬪€?/span>
ReactElement
ReactElement
灏嗕紶鍏ョ殑鍑犱釜灞炴€ц繘琛岀粍鍚堬紝骞惰繑鍥炪€?/p>
type
锛氬厓绱犵殑绫诲瀷锛屽彲浠ユ槸鍘熺敓html绫诲瀷锛堝瓧绗︿覆锛夛紝鎴栬€呰嚜瀹氫箟缁勪欢锛堝嚱鏁版垨class
锛?/span>key
锛氱粍浠剁殑鍞竴鏍囪瘑锛岀敤浜?nbsp;Diff
绠楁硶锛屼笅闈細璇︾粏浠嬬粛ref
锛氱敤浜庤闂師鐢?nbsp;dom
鑺傜偣props
锛氫紶鍏ョ粍浠剁殑props
owner
锛氬綋鍓嶆鍦ㄦ瀯寤虹殑Component
鎵€灞炵殑Component
$$typeof
锛氫竴涓垜浠笉甯歌鍒扮殑灞炴€э紝瀹冭璧嬪€间负 REACT_ELEMENT_TYPE
锛?/p>
var REACT_ELEMENT_TYPE =
(typeof Symbol === 'function' && Symbol.for && Symbol.for('react.element')) ||
0xeac7;
鍙锛?$$typeof
鏄竴涓?Symbol
绫诲瀷鐨勫彉閲忥紝杩欎釜鍙橀噺鍙互闃叉 XSS
銆?/p>
濡傛灉浣犵殑鏈嶅姟鍣ㄦ湁涓€涓紡娲烇紝鍏佽鐢ㄦ埛瀛樺偍浠绘剰 JSON
瀵硅薄锛?nbsp;鑰屽鎴风浠g爜闇€瑕佷竴涓瓧绗︿覆锛岃繖鍙兘浼氭垚涓轰竴涓棶棰橈細
// JSON
let expectedTextButGotJSON = {
type: 'div',
props: {
dangerouslySetInnerHTML: {
__html: '/* put your exploit here */'
},
},
};
let message = { text: expectedTextButGotJSON };
<p>
{message.text}
</p>
JSON
涓笉鑳藉瓨鍌?Symbol
绫诲瀷鐨勫彉閲忋€?/p>
ReactElement.isValidElement
鍑芥暟鐢ㄦ潵鍒ゆ柇涓€涓?React
缁勪欢鏄惁鏄湁鏁堢殑锛屼笅闈㈡槸瀹冪殑鍏蜂綋瀹炵幇銆?/p>
ReactElement.isValidElement = function (object) {
return typeof object === 'object' && object !== null && object.$$typeof === REACT_ELEMENT_TYPE;
};
鍙 React
娓叉煋鏃朵細鎶婃病鏈?$$typeof
鏍囪瘑锛屼互鍙婅鍒欐牎楠屼笉閫氳繃鐨勭粍浠惰繃婊ゆ帀銆?/p>
褰撲綘鐨勭幆澧冧笉鏀寔 Symbol
鏃讹紝 $$typeof
琚祴鍊间负 0xeac7
锛岃嚦浜庝负浠€涔堬紝 React
寮€鍙戣€呯粰鍑轰簡绛旀锛?/p>
0xeac7
鐪嬭捣鏉ユ湁鐐瑰儚React
銆?/p>
self
銆?source
鍙湁鍦ㄩ潪鐢熶骇鐜鎵嶄細琚姞鍏ュ璞′腑銆?/p>
self
鎸囧畾褰撳墠浣嶄簬鍝釜缁勪欢瀹炰緥銆?/span>_source
鎸囧畾璋冭瘯浠g爜鏉ヨ嚜鐨勬枃浠?fileName
)鍜屼唬鐮佽鏁?lineNumber
)銆?/span>
铏氭嫙DOM杞崲涓虹湡瀹濪OM
涓婇潰鎴戜滑鍒嗘瀽浜嗕唬鐮佽浆鎹㈡垚浜嗚櫄鎷?DOM
鐨勮繃绋嬶紝涓嬮潰鏉ョ湅涓€涓?React
濡備綍灏嗚櫄鎷?DOM
杞崲鎴愮湡瀹?DOM
銆?/p>
鏈儴鍒嗛€昏緫杈冨鏉傦紝鎴戜滑鍏堢敤娴佺▼鍥炬⒊鐞嗕竴涓嬫暣涓繃绋嬶紝鏁翠釜杩囩▼澶ф鍙垎涓哄洓姝ワ細
杩囩▼1锛氬垵濮嬪弬鏁板鐞?/strong>
鍦ㄧ紪鍐欏ソ鎴戜滑鐨?React
缁勪欢鍚庯紝鎴戜滑闇€瑕佽皟鐢?ReactDOM.render(element,container[,callback])
灏嗙粍浠惰繘琛屾覆鏌撱€?/p>
render
鍑芥暟鍐呴儴瀹為檯璋冪敤浜?_renderSubtreeIntoContainer
锛屾垜浠潵鐪嬬湅瀹冪殑鍏蜂綋瀹炵幇锛?/p>
render: function (nextElement, container, callback) {
return ReactMount._renderSubtreeIntoContainer(null, nextElement, container, callback);
},
1.灏嗗綋鍓嶇粍浠朵娇鐢?nbsp;TopLevelWrapper
杩涜鍖呰9
TopLevelWrapper
鍙竴涓┖澹筹紝瀹冧负浣犻渶瑕佹寕杞界殑缁勪欢鎻愪緵浜嗕竴涓?rootID
灞炴€э紝骞跺湪 render
鍑芥暟涓繑鍥炶缁勪欢銆?/p>
TopLevelWrapper.prototype.render = function () {
return this.props.child;
};
ReactDOM.render
鍑芥暟鐨勭涓€涓弬鏁板彲浠ユ槸鍘熺敓 DOM
涔熷彲浠ユ槸 React
缁勪欢锛屽寘瑁逛竴灞?TopLevelWrapper
鍙互鍦ㄥ悗闈㈢殑娓叉煋涓皢瀹冧滑杩涜缁熶竴澶勭悊锛岃€屼笉鐢ㄥ叧蹇冩槸鍚﹀師鐢熴€?/p>
2.鍒ゆ柇鏍圭粨鐐逛笅鏄惁宸茬粡娓叉煋杩囧厓绱狅紝濡傛灉宸茬粡娓叉煋杩囷紝鍒ゆ柇鎵ц鏇存柊鎴栬€呭嵏杞芥搷浣?/span>
3.澶勭悊
shouldReuseMarkup
鍙橀噺锛岃鍙橀噺琛ㄧず鏄惁闇€瑕侀噸鏂版爣璁板厓绱?/span>4.璋冪敤灏嗕笂闈㈠鐞嗗ソ鐨勫弬鏁颁紶鍏?nbsp;
_renderNewRootComponent
锛屾覆鏌撳畬鎴愬悗璋冪敤callback
銆?/span>
鍦?_renderNewRootComponent
涓皟鐢?instantiateReactComponent
瀵规垜浠紶鍏ョ殑缁勪欢杩涜鍒嗙被鍖呰锛?/p>
鏍规嵁缁勪欢鐨勭被鍨嬶紝 React
鏍规嵁鍘熺粍浠跺垱寤轰簡涓嬮潰鍥涘ぇ绫荤粍浠讹紝瀵圭粍浠惰繘琛屽垎绫绘覆鏌擄細
ReactDOMEmptyComponent
:绌虹粍浠?/span>ReactDOMTextComponent
:鏂囨湰ReactDOMComponent
:鍘熺敓DOM
ReactCompositeComponent
:鑷畾涔?nbsp;React
缁勪欢
浠栦滑閮藉叿澶囦互涓嬩笁涓柟娉曪細
construct
:鐢ㄦ潵鎺ユ敹ReactElement
杩涜鍒濆鍖栥€?/span>mountComponent
:鐢ㄦ潵鐢熸垚ReactElement
瀵瑰簲鐨勭湡瀹?nbsp;DOM
鎴?nbsp;DOMLazyTree
銆?/span>unmountComponent
:鍗歌浇DOM
鑺傜偣锛岃В缁戜簨浠躲€?/span>
鍏蜂綋鏄浣曟覆鏌撴垜浠湪杩囩▼3涓繘琛屽垎鏋愩€?/p>
杩囩▼2锛氭壒澶勭悊銆佷簨鍔¤皟鐢?/strong>
鍦?_renderNewRootComponent
涓娇鐢?ReactUpdates.batchedUpdates
璋冪敤 batchedMountComponentIntoNode
杩涜鎵瑰鐞嗐€?/p>
ReactUpdates.batchedUpdates(batchedMountComponentIntoNode, componentInstance, container, shouldReuseMarkup, context);
鍦?batchedMountComponentIntoNode
涓紝浣跨敤 transaction.perform
璋冪敤 mountComponentIntoNode
璁╁叾鍩轰簬浜嬪姟鏈哄埗杩涜璋冪敤銆?/p>
transaction.perform(mountComponentIntoNode, null, componentInstance, container, transaction, shouldReuseMarkup, context);
鍏充簬鎵瑰鐞嗕簨鍔★紝鍦ㄦ垜鍓嶉潰鐨勫垎鏋恠etState鎵ц鏈哄埗涓湁鏇村浠嬬粛銆?/p>
杩囩▼3锛氱敓鎴恏tml
鍦?mountComponentIntoNode
鍑芥暟涓皟鐢?ReactReconciler.mountComponent
鐢熸垚鍘熺敓 DOM
鑺傜偣銆?/p>
mountComponent
鍐呴儴瀹為檯涓婃槸璋冪敤浜嗚繃绋?鐢熸垚鐨勫洓绉嶅璞$殑 mountComponent
鏂规硶銆傞鍏堟潵鐪嬩竴涓?nbsp;ReactDOMComponent
锛?/p>
1.瀵圭壒娈?nbsp;DOM
鏍囩銆?nbsp;props
杩涜澶勭悊銆?/span>
2.鏍规嵁鏍囩绫诲瀷鍒涘缓 DOM
鑺傜偣銆?/span>
3.璋冪敤 _updateDOMProperties
灏?nbsp;props
鎻掑叆鍒?nbsp;DOM
鑺傜偣锛?nbsp;_updateDOMProperties
涔熷彲鐢ㄤ簬 propsDiff
锛岀涓€涓弬鏁颁负涓婃娓叉煋鐨?nbsp;props
锛岀浜屼釜鍙傛暟涓哄綋鍓?nbsp;props
锛岃嫢绗竴涓弬鏁颁负绌猴紝鍒欎负棣栨鍒涘缓銆?/span>
4.鐢熸垚涓€涓?nbsp;DOMLazyTree
瀵硅薄骞惰皟鐢?nbsp;_createInitialChildren
灏嗗瀛愯妭鐐规覆鏌撳埌涓婇潰銆?/span>
閭d箞涓轰粈涔堜笉鐩存帴鐢熸垚涓€涓?DOM
鑺傜偣鑰屾槸瑕佸垱寤轰竴涓?DOMLazyTree
鍛紵鎴戜滑鍏堟潵鐪嬬湅 _createInitialChildren
鍋氫簡浠€涔堬細
鍒ゆ柇褰撳墠鑺傜偣鐨?dangerouslySetInnerHTML
灞炴€с€佸瀛愯妭鐐规槸鍚︿负鏂囨湰鍜屽叾浠栬妭鐐瑰垎鍒皟鐢?DOMLazyTree
鐨?queueHTML
銆?queueText
銆?queueChild
銆?/p>
鍙互鍙戠幇锛?nbsp;DOMLazyTree
瀹為檯涓婃槸涓€涓寘瑁瑰璞★紝 node
灞炴€т腑瀛樺偍浜嗙湡瀹炵殑 DOM
鑺傜偣锛?children
銆?html
銆?text
鍒嗗埆瀛樺偍瀛╁瓙銆乭tml鑺傜偣鍜屾枃鏈妭鐐广€?/p>
瀹冩彁渚涗簡鍑犱釜鏂规硶鐢ㄤ簬鎻掑叆瀛╁瓙銆?html
浠ュ強鏂囨湰鑺傜偣锛岃繖浜涙彃鍏ラ兘鏄湁鏉′欢闄愬埗鐨勶紝褰?enableLazy
灞炴€т负 true
鏃讹紝杩欎簺瀛╁瓙銆?html
浠ュ強鏂囨湰鑺傜偣浼氳鎻掑叆鍒?DOMLazyTree
瀵硅薄涓紝褰撳叾涓?false
鏃朵細鎻掑叆鍒扮湡瀹?DOM
鑺傜偣涓€?/p>
var enableLazy = typeof document !== 'undefined' &&
typeof document.documentMode === 'number' ||
typeof navigator !== 'undefined' &&
typeof navigator.userAgent === 'string' &&
/Edge/d/.test(navigator.userAgent);
鍙锛?nbsp;enableLazy
鏄竴涓彉閲忥紝褰撳墠娴忚鍣ㄦ槸 IE
鎴?Edge
鏃朵负 true
銆?/p>
鍦?IE锛?/span>8-11锛?/span>
鍜?Edge
娴忚鍣ㄤ腑锛屼竴涓竴涓彃鍏ユ棤瀛愬瓩鐨勮妭鐐癸紝鏁堢巼瑕佽繙楂樹簬鎻掑叆涓€鏁翠釜搴忓垪鍖栧畬鏁寸殑鑺傜偣鏍戙€?/p>
鎵€浠?lazyTree
涓昏瑙e喅鐨勬槸鍦?IE锛?/span>8-11锛?/span>
鍜?Edge
娴忚鍣ㄤ腑鎻掑叆鑺傜偣鐨勬晥鐜囬棶棰橈紝鍦ㄥ悗闈㈢殑杩囩▼4鎴戜滑浼氬垎鏋愬埌锛氳嫢褰撳墠鏄?nbsp;IE
鎴?Edge
锛屽垯闇€瑕侀€掑綊鎻掑叆 DOMLazyTree
涓紦瀛樼殑瀛愯妭鐐癸紝鍏朵粬娴忚鍣ㄥ彧闇€瑕佹彃鍏ヤ竴娆″綋鍓嶈妭鐐癸紝鍥犱负浠栦滑鐨勫瀛愬凡缁忚娓叉煋濂戒簡锛岃€屼笉鐢ㄦ媴蹇冩晥鐜囬棶棰樸€?/p>
涓嬮潰鏉ョ湅涓€涓?ReactCompositeComponent
锛岀敱浜庝唬鐮侀潪甯稿杩欓噷灏变笉鍐嶈创杩欎釜妯″潡鐨勪唬鐮侊紝鍏跺唴閮ㄤ富瑕佸仛浜嗕互涓嬪嚑姝ワ細
澶勭悊
props
銆?nbsp;contex
绛夊彉閲忥紝璋冪敤鏋勯€犲嚱鏁板垱寤虹粍浠跺疄渚?/span>鍒ゆ柇鏄惁涓烘棤鐘舵€佺粍浠讹紝澶勭悊
state
璋冪敤
performInitialMount
鐢熷懡鍛ㄦ湡锛屽鐞嗗瓙鑺傜偣锛岃幏鍙?nbsp;markup
銆?/span>璋冪敤
componentDidMount
鐢熷懡鍛ㄦ湡
鍦?performInitialMount
鍑芥暟涓紝棣栧厛璋冪敤浜?componentWillMount
鐢熷懡鍛ㄦ湡锛岀敱浜庤嚜瀹氫箟鐨?React
缁勪欢骞朵笉鏄竴涓湡瀹炵殑DOM锛屾墍浠ュ湪鍑芥暟涓張璋冪敤浜嗗瀛愯妭鐐圭殑 mountComponent
銆傝繖涔熸槸涓€涓€掑綊鐨勮繃绋嬶紝褰撴墍鏈夊瀛愯妭鐐规覆鏌撳畬鎴愬悗锛岃繑鍥?nbsp;markup
骞惰皟鐢?componentDidMount
銆?/p>
杩囩▼4锛氭覆鏌揾tml
鍦?mountComponentIntoNode
鍑芥暟涓皟鐢ㄥ皢涓婁竴姝ョ敓鎴愮殑 markup
鎻掑叆 container
瀹瑰櫒銆?/p>
鍦ㄩ娆℃覆鏌撴椂锛?_mountImageIntoNode
浼氭竻绌?container
鐨勫瓙鑺傜偣鍚庤皟鐢?DOMLazyTree.insertTreeBefore
锛?/p>
鍒ゆ柇鏄惁涓?fragment
鑺傜偣鎴栬€?<object>
鎻掍欢锛?/p>
濡傛灉鏄互涓婁袱绉嶏紝棣栧厛璋冪敤
insertTreeChildren
灏嗘鑺傜偣鐨勫瀛愯妭鐐规覆鏌撳埌褰撳墠鑺傜偣涓婏紝鍐嶅皢娓叉煋瀹岀殑鑺傜偣鎻掑叆鍒?html
濡傛灉鏄叾浠栬妭鐐癸紝鍏堝皢鑺傜偣鎻掑叆鍒版彃鍏ュ埌
html
锛屽啀璋冪敤insertTreeChildren
灏嗗瀛愯妭鐐规彃鍏ュ埌html
銆?/p>鑻ュ綋鍓嶄笉鏄?
IE
鎴?Edge
锛屽垯涓嶉渶瑕佸啀閫掑綊鎻掑叆瀛愯妭鐐癸紝鍙渶瑕佹彃鍏ヤ竴娆″綋鍓嶈妭鐐广€?/p>
鍒ゆ柇涓嶆槸
IE
鎴?nbsp;bEdge
鏃?nbsp;return
鑻?nbsp;
children
涓嶄负绌猴紝閫掑綊insertTreeBefore
杩涜鎻掑叆娓叉煋html鑺傜偣
娓叉煋鏂囨湰鑺傜偣
鍘熺敓DOM浜嬩欢浠g悊
鏈夊叧铏氭嫙 DOM
鐨勪簨浠舵満鍒讹紝鎴戞浘涓撻棬鍐欒繃涓€绡囨枃绔狅紝鏈夊叴瓒e彲浠ヰ煈囥€怰eact娣卞叆銆慠eact浜嬩欢鏈哄埗
铏氭嫙DOM鍘熺悊銆佺壒鎬ф€荤粨
React缁勪欢鐨勬覆鏌撴祦绋?/h3>
浣跨敤 React.createElement
鎴?JSX
缂栧啓 React
缁勪欢锛屽疄闄呬笂鎵€鏈夌殑 JSX
浠g爜鏈€鍚庨兘浼氳浆鎹㈡垚 React.createElement(...)
锛?Babel
甯姪鎴戜滑瀹屾垚浜嗚繖涓浆鎹㈢殑杩囩▼銆?/p>
createElement
鍑芥暟瀵?key
鍜?ref
绛夌壒娈婄殑 props
杩涜澶勭悊锛屽苟鑾峰彇 defaultProps
瀵归粯璁?props
杩涜璧嬪€硷紝骞朵笖瀵逛紶鍏ョ殑瀛╁瓙鑺傜偣杩涜澶勭悊锛屾渶缁堟瀯閫犳垚涓€涓?ReactElement
瀵硅薄锛堟墍璋撶殑铏氭嫙 DOM
锛夈€?/p>
ReactDOM.render
灏嗙敓鎴愬ソ鐨勮櫄鎷?DOM
娓叉煋鍒版寚瀹氬鍣ㄤ笂锛屽叾涓噰鐢ㄤ簡鎵瑰鐞嗐€佷簨鍔$瓑鏈哄埗骞朵笖瀵圭壒瀹氭祻瑙堝櫒杩涜浜嗘€ц兘浼樺寲锛屾渶缁堣浆鎹负鐪熷疄 DOM
銆?/p>
铏氭嫙DOM鐨勭粍鎴?/h3>
浣跨敤 React.createElement
鎴?JSX
缂栧啓 React
缁勪欢锛屽疄闄呬笂鎵€鏈夌殑 JSX
浠g爜鏈€鍚庨兘浼氳浆鎹㈡垚 React.createElement(...)
锛?Babel
甯姪鎴戜滑瀹屾垚浜嗚繖涓浆鎹㈢殑杩囩▼銆?/p>
createElement
鍑芥暟瀵?key
鍜?ref
绛夌壒娈婄殑 props
杩涜澶勭悊锛屽苟鑾峰彇 defaultProps
瀵归粯璁?props
杩涜璧嬪€硷紝骞朵笖瀵逛紶鍏ョ殑瀛╁瓙鑺傜偣杩涜澶勭悊锛屾渶缁堟瀯閫犳垚涓€涓?ReactElement
瀵硅薄锛堟墍璋撶殑铏氭嫙 DOM
锛夈€?/p>
ReactDOM.render
灏嗙敓鎴愬ソ鐨勮櫄鎷?DOM
娓叉煋鍒版寚瀹氬鍣ㄤ笂锛屽叾涓噰鐢ㄤ簡鎵瑰鐞嗐€佷簨鍔$瓑鏈哄埗骞朵笖瀵圭壒瀹氭祻瑙堝櫒杩涜浜嗘€ц兘浼樺寲锛屾渶缁堣浆鎹负鐪熷疄 DOM
銆?/p>
鍗?ReactElement
element瀵硅薄锛屾垜浠殑缁勪欢鏈€缁堜細琚覆鏌撴垚涓嬮潰鐨勭粨鏋勶細
type
锛氬厓绱犵殑绫诲瀷锛屽彲浠ユ槸鍘熺敓html绫诲瀷锛堝瓧绗︿覆锛夛紝鎴栬€呰嚜瀹氫箟缁勪欢锛堝嚱鏁版垨class
锛?/span>key
锛氱粍浠剁殑鍞竴鏍囪瘑锛岀敤浜?nbsp;Diff
绠楁硶锛屼笅闈細璇︾粏浠嬬粛ref
锛氱敤浜庤闂師鐢?nbsp;dom
鑺傜偣props
锛氫紶鍏ョ粍浠剁殑props
锛?nbsp;chidren
鏄?nbsp;props
涓殑涓€涓睘鎬э紝瀹冨瓨鍌ㄤ簡褰撳墠缁勪欢鐨勫瀛愯妭鐐癸紝鍙互鏄暟缁勶紙澶氫釜瀛╁瓙鑺傜偣锛夋垨瀵硅薄锛堝彧鏈変竴涓瀛愯妭鐐癸級owner
锛氬綋鍓嶆鍦ㄦ瀯寤虹殑Component
鎵€灞炵殑Component
self
锛氾紙闈炵敓浜х幆澧冿級鎸囧畾褰撳墠浣嶄簬鍝釜缁勪欢瀹炰緥_source
锛氾紙闈炵敓浜х幆澧冿級鎸囧畾璋冭瘯浠g爜鏉ヨ嚜鐨勬枃浠?fileName
)鍜屼唬鐮佽鏁?lineNumber
)
闃叉XSS
ReactElement
瀵硅薄杩樻湁涓€涓?$$typeof
灞炴€э紝瀹冩槸涓€涓?Symbol
绫诲瀷鐨勫彉閲?Symbol.for('react.element')
锛屽綋鐜涓嶆敮鎸?Symbol
鏃讹紝 $$typeof
琚祴鍊间负 0xeac7
銆?/p>
杩欎釜鍙橀噺鍙互闃叉 XSS
銆傚鏋滀綘鐨勬湇鍔″櫒鏈変竴涓紡娲烇紝鍏佽鐢ㄦ埛瀛樺偍浠绘剰 JSON
瀵硅薄锛?nbsp;鑰屽鎴风浠g爜闇€瑕佷竴涓瓧绗︿覆锛岃繖鍙兘涓轰綘鐨勫簲鐢ㄧ▼搴忓甫鏉ラ闄┿€?nbsp;JSON
涓笉鑳藉瓨鍌?Symbol
绫诲瀷鐨勫彉閲忥紝鑰?React
娓叉煋鏃朵細鎶婃病鏈?$$typeof
鏍囪瘑鐨勭粍浠惰繃婊ゆ帀銆?/p>
鎵瑰鐞嗗拰浜嬪姟
React
鍦ㄦ覆鏌撹櫄鎷?DOM
鏃跺簲鐢ㄤ簡鎵瑰鐞嗕互鍙婁簨鍔℃満鍒讹紝浠ユ彁楂樻覆鏌撴€ц兘銆?/p>
鍏充簬鎵瑰鐞嗕互鍙婁簨鍔℃満鍒讹紝鍦ㄦ垜涔嬪墠鐨勬枃绔犮€怰eact娣卞叆銆憇etState鐨勬墽琛屾満鍒朵腑鏈夎缁嗕粙缁嶃€?/p>
閽堝鎬х殑鎬ц兘浼樺寲
鍦?IE锛?/span>8-11锛?/span>
鍜?Edge
娴忚鍣ㄤ腑锛屼竴涓竴涓彃鍏ユ棤瀛愬瓩鐨勮妭鐐癸紝鏁堢巼瑕佽繙楂樹簬鎻掑叆涓€鏁翠釜搴忓垪鍖栧畬鏁寸殑鑺傜偣鏍戙€?/p>
React
閫氳繃 lazyTree
锛屽湪 IE锛?/span>8-11锛?/span>
鍜?Edge
涓繘琛屽崟涓妭鐐逛緷娆℃覆鏌撹妭鐐癸紝鑰屽湪鍏朵粬娴忚鍣ㄤ腑鍒欓鍏堝皢鏁翠釜澶х殑 DOM
缁撴瀯鏋勫缓濂斤紝鐒跺悗鍐嶆暣浣撴彃鍏ュ鍣ㄣ€?/p>
骞朵笖锛屽湪鍗曠嫭娓叉煋鑺傜偣鏃讹紝 React
杩樿€冭檻浜?fragment
绛夌壒娈婅妭鐐癸紝杩欎簺鑺傜偣鍒欎笉浼氫竴涓竴涓彃鍏ユ覆鏌撱€?/p>
铏氭嫙DOM浜嬩欢鏈哄埗
React
鑷繁瀹炵幇浜嗕竴濂椾簨浠舵満鍒讹紝鍏跺皢鎵€鏈夌粦瀹氬湪铏氭嫙 DOM
涓婄殑浜嬩欢鏄犲皠鍒扮湡姝g殑 DOM
浜嬩欢锛屽苟灏嗘墍鏈夌殑浜嬩欢閮戒唬鐞嗗埌 document
涓婏紝鑷繁妯℃嫙浜嗕簨浠跺啋娉″拰鎹曡幏鐨勮繃绋嬶紝骞朵笖杩涜缁熶竴鐨勪簨浠跺垎鍙戙€?/p>
React
鑷繁鏋勯€犱簡鍚堟垚浜嬩欢瀵硅薄 SyntheticEvent
锛岃繖鏄竴涓法娴忚鍣ㄥ師鐢熶簨浠跺寘瑁呭櫒銆?nbsp;瀹冨叿鏈変笌娴忚鍣ㄥ師鐢熶簨浠剁浉鍚岀殑鎺ュ彛锛屽寘鎷?nbsp;stopPropagation()
鍜?preventDefault()
绛夌瓑锛屽湪鎵€鏈夋祻瑙堝櫒涓粬浠伐浣滄柟寮忛兘鐩稿悓銆傝繖鎶瑰钩浜嗗悇涓祻瑙堝櫒鐨勪簨浠跺吋瀹规€ч棶棰樸€?/p>
涓婇潰鍒嗘瀽浜嗚櫄鎷?DOM
鐨勯儴鍒嗗師鐞嗭紝褰撶劧杩欏苟涓嶅寘鎷櫄鎷?DOM
杩涜 Diff
鐨勮繃绋嬶紝涓嬩竴绡囨枃绔犳垜浠啀鏉ヨ缁嗘帰璁ㄣ€?/p>
鏈熬
鍏充簬寮€绡囨彁鐨勫嚑涓棶棰橈紝鎴戜滑鍦ㄤ笅绡囨枃绔犱腑杩涜缁熶竴鍥炵瓟銆?/p>
鏂囦腑濡傛湁閿欒锛屾杩庡湪璇勮鍖烘寚姝o紝鎴栬€呮偍瀵规枃绔犵殑鎺掔増锛岄槄璇讳綋楠屾湁浠€涔堝ソ鐨勫缓璁紝娆㈣繋鍦ㄨ瘎璁哄尯鎸囧嚭锛岃阿璋㈤槄璇汇€?/p>
以上是关于的主要内容,如果未能解决你的问题,请参考以下文章