admin管理员组文章数量:1403226
I am creating a react ponent using
React.render(<ReactComponent data="myData">, document.body);
Once the data model changes, I call render again using
React.render(<ReactComponent data="myData">, document.body);
Is this the right/remended way to update my HTML?
Will this utilize the advantages of the React virtual DOM (i.e. rendering only the elements that have actually changed).
Also, should I be using state or properties when passing in myData?
I am creating a react ponent using
React.render(<ReactComponent data="myData">, document.body);
Once the data model changes, I call render again using
React.render(<ReactComponent data="myData">, document.body);
Is this the right/remended way to update my HTML?
Will this utilize the advantages of the React virtual DOM (i.e. rendering only the elements that have actually changed).
Also, should I be using state or properties when passing in myData?
Share Improve this question edited Feb 1, 2017 at 17:50 user692942 16.4k8 gold badges84 silver badges190 bronze badges asked Apr 17, 2015 at 14:05 Amit BehereAmit Behere 821 silver badge6 bronze badges1 Answer
Reset to default 7You should be rendering only one main App ponent which does AJAX requests etc and uses the data model inside its render function to update sub ponents.
When creating React ponents you should always keep the use of state minimal and move it up to the top level ponent, Instead you should use props to render child ponents.
This article helped me a lot when i was first getting started with React: https://github./uberVU/react-guide/blob/master/props-vs-state.md
so something like:
var App = React.createClass({
render: function(){
return (
<div>
<input type="button" onClick={this.handleClick}/>
<Dropdown items={this.state.countries}/>
</div>
)
},
getInitialState: function(){
return {countries: {}};
},
ponentDidMount: function(){
var self = this;
$.getJSON("countries", function(err, countries){
self.setState({countries: countries});
});
},
handleClick: function(){
// every time the user does something,
// all you need to do is to update the state of the App
// which is passed as props to sub ponents
}
})
React.render(React.createElement(App, {}), document.body);
本文标签: javascriptHow to trigger rerender on model change in ReactJSStack Overflow
版权声明:本文标题:javascript - How to trigger re-render on model change in ReactJS? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744348685a2601920.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论