【发布时间】:2018-03-11 08:13:33
【问题描述】:
我们的产品详情页面在单个页面中包含多个组件。
产品组件如下所示:
class Product extends Component {
render() {
return (
<div>
<Searchbar/>
<Gallery/>
<Video/>
<Details/>
<Contact/>
<SimilarProd/>
<OtherProd/>
</div>
);
}
}
这里我们有 3 个 API 用于 - 细节 - 类似产品 - 其他产品
现在我们需要从Detail API 为这些组件设置数据
<Gallery/>
<Video/>
<Details/>
<Contact/>
我们需要在哪个组件中调用API,以及如何给其他组件设置数据。假设我们需要为每个组件分配 a,b,c,d 值
componentWillMount(props) {
fetch('/deatail.json').then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error('Something went wrong ...');
}
})
.then(data => this.setState({ data, isLoading: false }))
.catch(error => this.setState({ error, isLoading: false }));
}
或
我们需要为每个组件创建单独的 api 吗?
【问题讨论】:
标签: reactjs components