I'm new to React.js and struggling to understand few core concepts to decide should we use this library for our application. My main problem is actually handling update in model that fetched from server.
Imagine, that I have a page that should display five different models. I've built it in the way described in this article: http://facebook.github.io/react/blog/2013/11/05/thinking-in-react.html, so I have "root" component where all the 5 models passed and using props they are going down to the components that hold this models. So, now 2 models updated (I get this events from my model code that live outside of react components) and I need to reflect this on UI. What is the best way to do this?
I'm thinking about following options:
Thank you in advance and hope I was able explain my problem clearly.
There is a variety of ways to fetch data in React, including using the built-in Fetch API, Axios, async/await, and more. We'll go over all these methods in detail. You can also fetch data in higher-order components and render props, from a GraphQL backend, and more.
Methods to send response from server to client are:Using send() function. Using json() function.
Calling renderComponent again with the same component but different data is equivalent to calling component.setProps(). So either keep all the models as state in the least common denominator, or just call setProps/renderComponent again when it changes.
If you pass the data as props down to your child component, you can simply update it at a higher level and it will force a render to all components that uses the same property object. Consider this simple example:
var World = React.createClass({
render: function() {
return <strong>{this.props.name}</strong>;
}
});
var Hello = React.createClass({
clickHandler: function() {
this.setProps({ name: 'earth' });
},
render: function() {
return (
<div>
Hello <World name={this.props.name} />
<button onClick={this.clickHandler}>Click me</button>
</div>
);
}
});
Now, when the user clicks the button you change the property on the Hello
component, but since you passed the same property (or data) object to the children, they will react to it and update it’s shadow DOM accordingly.
Here is a fiddle of what I mean: http://jsfiddle.net/xkCKR/
If you have an external data object, you can just pass it to the top component. Just remember that this doesn’t mean that there is a two-way binding:
// simple example of a data model
var Data = { name: 'world' };
var World = React.createClass({
render: function() {
return <strong>{this.props.data.name}</strong>;
}
});
var Hello = React.createClass({
clickHandler: function() {
this.setProps({
data: { name: 'earth' }
});
},
render: function() {
return (
<div>
Hello <World data={this.props.data} />
<button onClick={this.clickHandler}>Click me</button>
</div>
);
}
});
React.renderComponent(<Hello data={Data} />, document.body);
This works because react uses one-way binding of properties. But if say your child component would update it’s properties, it won’t climb up to it’s parent. For that you’ll need the ReactLink add-on or use a pub/sub interface like the one Backbone provides.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With