【发布时间】:2018-07-11 16:27:42
【问题描述】:
我曾经在我的组件中获取数据,但现在我了解了更高阶的组件,我正在尝试重构我的代码以使用 hoc 来获取数据;
从原始组件如何将我从路由器道具获取的 url 移动到 hoc 包装器
state = { details: [], loading: true };
componentDidMount() {
fetch(this.props.match.url)
.then(res => res.json())
.then(data => this.setState({ details: data, loading: false }))
}
我如何将this.props.match.url 传递给export default withData(App, __here__)?
与数据:
export const withData = (ComposedComponent, url) => {
return class WithData extends Component {
state = { data: [], loading: false };
componentDidMount() {
this.setState({ loading: true });
fetch(url)
.then(response => response.json())
.then(data => this.setState({ loading: false, data }));
}
render() {
return <ComposedComponent {...this.state} {...this.props} />;
}
};
};
【问题讨论】:
-
你能分享
withData的代码吗? -
刚刚包含在问题中!
标签: reactjs react-router