Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
569 views
in Technique[技术] by (71.8m points)

reactjs - React: leave the contents of a component alone

Is it possible to have React ignore a subtree? i.e., don't compare or update it?

My use case is migrating to React. Re-writing all our Handlebars templates at once isn't feasible, but if we could use our existing templates for some subcomponents, we could adopt it slowly over time.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, if you don't modify a subtree within React then the DOM won't be touched at all. It's easy to wrap non-React functionality like a Handlebars template in React. You can either use dangerouslySetInnerHTML:

render: function() 
    return <div dangerouslySetInnerHTML={{__html: template(values)}}>;
}

or you can simply return an empty div and populate (or attach event handlers, etc) it in componentDidMount:

render: function() 
    return <div />;
},
componentDidMount: function() {
    var node = React.findDOMNode(this);
    node.innerHTML = template(values);
}

In the latter case, React won't touch the DOM after the initial render because render always returns the same thing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...