简体   繁体   English

ReactJS渲染函数是注入全部还是部分添加的DOM元素?

[英]Is ReactJS render function injecting DOM elements in full, or partial additions?

I am going through the official React Tutorial at https://facebook.github.io/react/docs/tutorial.html 我正在通过https://facebook.github.io/react/docs/tutorial.html浏览官方的React教程

 var CommentBox = React.createClass({ loadCommentsFromServer: function() { $.ajax({ url: this.props.url, dataType: 'json', cache: false, success: function(data) { this.setState({ data: data }); }.bind(this), error: function(xhr, status, err) { console.error(this.props.url, status, err.toString()); }.bind(this) }) }, handleCommentSubmit: function(comment) { $.ajax({ url: this.props.url, dataType: 'json', type: 'POST', data: comment, success: function(data) { this.setState({ data: data }); }.bind(this), error: function(xhr, status, err) { console.error(this.props.url, status, err.toString()); // body... } }); }, getInitialState: function() { return { data: [] }; }, componentDidMount: function() { this.loadCommentsFromServer(); setInterval(this.loadCommentsFromServer, this.props.pollInterval); }, render: function() { return ( < div className = "commentBox" > < h1 > Comments < /h1> <CommentList data={this.state.data} / > < CommentForm onCommentSubmit = { this.handleCommentSubmit } /> </div > ); } }) 

This is only one but major part of the comment rendering. 这只是注释渲染的一个部分,但很重要。 In the render function, does ReactJS destroy the previous DOM elements, and create a new DOM element tree based on XHR data received, or does it compare with the old tree and only add the diff s? 在render函数中,ReactJS会销毁先前的DOM元素,并根据接收到的XHR数据创建一个新的DOM元素树,还是将其与旧树进行比较,仅添加diff If it creates completely new elements, how does it do it so fast, as I am perceiving from the narrative of the tutorials that React does this exceptionally faster than other frameworks? 如果它创建了全新的元素,它会如何如此快地执行,正如我从教程的叙述中看到的那样,React的执行速度比其他框架快得多?

does ReactJS destroy the previous DOM elements, and create a new DOM element tree based on XHR data received, or does it compare with the old tree and only add the diffs? ReactJS会销毁先前的DOM元素,并根据收到的XHR数据创建一个新的DOM元素树,还是将其与旧树进行比较,仅添加差异?

React diffs/compares the new DOM tree with the previous(old), and only renders the diffs. React diff /将新的DOM树与上一个(旧)进行比较,仅渲染diff。

You find the explanation in the official API doc here . 您可以在此处的官方API文档中找到说明。

"Note: ReactDOM.render() controls the contents of the container node you pass in. Any existing DOM elements inside are replaced when first called. Later calls use React's DOM diffing algorithm for efficient updates. ReactDOM.render() does not modify the container node (only modifies the children of the container). In the future, it may be possible to insert a component to an existing DOM node without overwriting the existing children." “注意:ReactDOM.render()控制您传入的容器节点的内容。第一次调用时,内部的任何现有DOM元素都会被替换。以后的调用使用React的DOM差异算法进行有效的更新。ReactDOM.render()不会修改容器节点(仅修改容器的子节点)。将来,可以在不覆盖现有子节点的情况下将组件插入到现有DOM节点中。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM