react服务端渲染首次获取数据
安装
npm install --save fetch-initial-data
使用演示
需要首次加载的组件
import {bindActionCreators} from 'redux';
class Home extends Component{
constructor(props){
super(props)
//判断是否是浏览器
if(typeof window==="object")this.fetchData();
}
//fetchData固定名称,必须返回异步,且所有action需要awiat
async fetchData(){
await this.props.getDataAct();
}
render(){
let {data}=this.props;
return
首页 {data}
}
}
const mapStateToProps=(state)=>({
data:state.config.data
})
const mapDispatchToProps=(dispatch)=>bindActionCreators({
getDataAct:Acts.getData,
},dispatch)
export default connect(mapStateToProps,mapDispatchToPr