类似 vue 的 mixins
- 定义withFetch.jsx 文件, 内部使用公共部分
import React, {Component} from 'react';
// 公共部分放在这里, mixins
const withFetch = (count) => (View) => {
return class extends Component {
constructor(){
super()
this.state= {
loading: true,
count: 0
}
}
componentDidMount(){
this.setState({
count: count
})
setTimeout(()=>{
this.setState({
loading:false
})
}, 5000)
}
render() {
if(this.state.loading) {
return <div>loading.......</div>
}else{
return <View count={this.state.count}></View>
}
}
}
}
export default withFetch
import React from 'react';
import withFetch from './withFetch'
const Banner = withFetch(123)(props => {
return (
<div>
<p>
获取到数据:
{props.count}
</p>
</div>
)
})
export default Banner