我可以将 getInitialProps 代码移动到 componentDidMount 吗?
Can I move getInitialProps code to componentDidMount?
我想将static getInitialProps 中的代码移动到componentDidMount。
但是,getInitialProps 中有两个参数(component,ctx),我应该全部使用它们。
我只是想知道我是否可以移动代码。
谢谢!
不知道..
// Current
class extends App {
public static async getInitialProps({Component, ctx}) {
// Do Something with two parameters
}
.
.
.
}
// What I want to do
class extends App {
public componentDidMount() {
// Do the same thing
}
.
.
.
}
getInitialProps
在服务器端和客户端都有效,而 componentDidMount
仅适用于客户端 side.Therefore,你不能在 componentDidMount
.[=17 中使用它=]
如果你想在 componentDidMount 中使用 getInitialProps
中获取的数据,你可以 return 来自 getInitialProps
的数据并将其用作 prop
.
class extends App{
static async getInitialProps({ Component, ctx }) {
//do some data fetching
return { data };
}
constructor() {
super();
this.state = {};
}
componentDidMount() {
console.log(this.props.data)
}
}
我想将static getInitialProps 中的代码移动到componentDidMount。 但是,getInitialProps 中有两个参数(component,ctx),我应该全部使用它们。 我只是想知道我是否可以移动代码。 谢谢!
不知道..
// Current
class extends App {
public static async getInitialProps({Component, ctx}) {
// Do Something with two parameters
}
.
.
.
}
// What I want to do
class extends App {
public componentDidMount() {
// Do the same thing
}
.
.
.
}
getInitialProps
在服务器端和客户端都有效,而 componentDidMount
仅适用于客户端 side.Therefore,你不能在 componentDidMount
.[=17 中使用它=]
如果你想在 componentDidMount 中使用 getInitialProps
中获取的数据,你可以 return 来自 getInitialProps
的数据并将其用作 prop
.
class extends App{
static async getInitialProps({ Component, ctx }) {
//do some data fetching
return { data };
}
constructor() {
super();
this.state = {};
}
componentDidMount() {
console.log(this.props.data)
}
}