[英]Extend JSON elements through loop component included in AJAX component or not? - React JS
My aim is to write a component that will be able to be used in others, providing a list of products, categories, and descriptions from an endpoint. 我的目的是编写一个可以在其他应用程序中使用的组件,并提供来自端点的产品,类别和描述的列表。
So far so good (console.log), BUT is looping in the componentDidMount the best way to go about this? 到目前为止,一切都很好(console.log),但是BUT循环在componentDidMount中是实现此目的的最佳方法吗? Should I even loop it in GetData or do a forEach in another component that I want to use it in?
我应该在GetData中循环还是在要使用它的另一个组件中进行forEach?
I'm relatively new to React so still trying to work out the best approach to this. 我对React还是比较陌生,因此仍在尝试找出最佳方法。
JS: JS:
var GetData = React.createClass({
getInitialState: function() {
return {
categories: [],
productNames: [],
descriptions: []
};
},
componentDidMount: function() {
this.serverRequest = $.get("HTTPS://ENDPOINT", function (result) {
var products = result;
for (var i = 0; i < products.data.length; i++) {
this.setState({
categories : products.data[i].categories[0].title,
productNames : products.data[i].title,
descriptions : products.data[i].descriptions
});
}
}.bind(this));
},
componentWillUnmount: function() {
this.serverRequest.abort();
},
render: function() {
console.log(this.state.categories);
// console.log(this.state.productNames);
// console.log(this.state.descriptions);
return (
this.props.categories.forEach(function(category) {
// ??
},
this.props.products.forEach(function(product) {
// ??
},
this.props.descriptions.forEach(function(description) {
// ??
},
)
}
});
You can create a "smart" component that will be responsible only for making the Ajax Request without rendering anything in the UI apart from Categories
Products
and Descriptions
. 您可以创建一个“智能”组件,该组件仅负责发出Ajax请求,而无需在UI中呈现“
Categories
Products
和“ Descriptions
以外的任何内容。 Another thing that you can do is to move your ajax
request to componentWillMount
. 您可以做的另一件事是将
ajax
请求移至componentWillMount
。 So in your case I would write something like that : 所以在您的情况下,我会写这样的东西:
import Category from 'components/Category';
import Description from 'components/Description';
import Product from 'components/Product';
export default class AjaxRequestComponent extends Component
{
componentWillMount: function() {
$.get("HTTPS://ENDPOINT", function (result) {
var products = result;
for (var i = 0; i < products.data.length; i++) {
this.setState({
categories : products.data[i].categories[0].title,
productNames : products.data[i].title,
descriptions : products.data[i].descriptions
});
}
}.bind(this));
}
render(){
return(
{
this.state.descriptions.map( d => <Description {...d} />)
this.state.products.map( p => <Products {...p} />)
this.state.categories.map( c => <Category {...c} />)
}
)
}
}
Category
, Products
and Description
are "dumb" components only responsible for presenting the data that you pass to them. Category
, Products
和Description
是“哑”组件,仅负责显示传递给它们的数据。
componentDidMount: function() {
this.serverRequest = $.get("HTTPS://ENDPOINT", function (result) {
var products = result;
var categories = products.data.map(function(item){
return item.categories[0].title;
};
var productNames = products.data.map(function(item){
return item.title;
};
var descriptions= products.data.map(function(item){
return item.descriptions;
};
}
this.setState({
categories : categories ,
productNames : productNames ,
descriptions : descriptions
});
}.bind(this));
},
in render function u can still use map function to return Array of React Component like example below 在render函数中,您仍然可以使用map函数返回React Component数组,如下例所示
render: function() {
return (
<div>
{this.state.categories.map(function(category) {
return <span>{category}</span>
}}
</div>
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.