我正在编写一个HOC,它将从我的API中获取用户数据,然后将props传递给包装的组件,以便在用户没有正确角色时可以重定向用户。
我的HOC在下面,但是getInitialProps
函数不会触发。尽管myNum
道具被传递了下来,HOC仍在工作。
import React, { Component } from 'react';
function routeRedirect(C) {
return class Higher extends Component {
static async getInitialProps() {
console.log('GIP');
await fetch('/api/getUsers');
}
render() {
return <C {...this.props} myNum={42} />;
}
};
}
export default routeRedirect;
有什么想法我要去哪里吗?
您没有从getInitialProps
返回获取的数据。
在docs中,它说:“服务器渲染时,将从getInitialProps返回的数据进行序列化...”。