【发布时间】:2019-04-06 08:52:50
【问题描述】:
我有一个创建卡片滑块(卡片轮播)的要求。我已将组件拆分为 Slider 和 Card。然后我有一个容器“应用程序”。卡片的数据来自redux。还有一个选项可以刷新每张卡。目前,我已经在“App”容器中获取数据、更新数据操作。在 App 容器中,我做了以下事情。
- 调用获取数据操作。
- 获取数据作为道具。
- 通过它进行映射并将必要的信息传递到卡片中。
- 将这些卡片作为孩子传递给 .
- 刷新卡片时,我调用更新数据操作。
那么有必要将它们分开吗?还是现在的结构还好?
import React, { Component } from "react";
import { connect } from "react-redux";
import Slider from "../../Components/Slider";
import Card from "../../Components/Card";
import * as actions from "../../Store/Actions/app";
class App extends Component {
componentDidMount() {
this.props.onInitApp(data);
}
cardRefreshHandler = (e, countryName, countryId) => {
this.props.onLoading();
this.props.onCardRefresh(country, countryId);
};
render() {
let cards = "";
if (this.props.data && this.props.data.length > 0) {
cards = this.props.data.map((item, index) => {
return (
<Card
key={index}
id={index}
data={item}
onRefresh={this.cardRefreshHandler}
/>
);
});
}
return (
<div class="app-container">
<Slider>
{cards}
</Slider>
</div>
);
}
}
const mapStateToProps = state => {
return {
data: state.data,
loading: state.loading
};
};
const mapDispatchToProps = dispatch => {
return {
onInitApp: country => dispatch(actions.fetchData(country)),
onLoading: () => dispatch(actions.loadRefreshScreen()),
onCardRefresh: (country, countryId) => dispatch(actions.refreshData(country, countryId))
};
};
export default connect(mapStateToProps, mapDispatchToProps)(App);
【问题讨论】:
标签: javascript reactjs react-redux