Reactjs 如何在任何页面上将next.js getInitialProps方法与mobx一起使用?
我必须在一个项目中同时使用mobx和next.js。我已经查看了github上的官方文档“使用mobx”示例。所以,我明白了主要思想是服务器和客户端存储必须相同。每次在服务器连接上,我都应该创建新的存储。但我无法理解getInıtialProps方法中的代码。为什么我们在getInıtialProps方法中使用getInıtialProps两次?同样的代码也在构造函数中使用。你能解释一下背后的想法吗?我找不到解释性的例子。另外,我应该在每个页面上使用getInıtialProps还是可以使用inject和observerReactjs 如何在任何页面上将next.js getInitialProps方法与mobx一起使用?,reactjs,next.js,mobx,Reactjs,Next.js,Mobx,我必须在一个项目中同时使用mobx和next.js。我已经查看了github上的官方文档“使用mobx”示例。所以,我明白了主要思想是服务器和客户端存储必须相同。每次在服务器连接上,我都应该创建新的存储。但我无法理解getInıtialProps方法中的代码。为什么我们在getInıtialProps方法中使用getInıtialProps两次?同样的代码也在构造函数中使用。你能解释一下背后的想法吗?我找不到解释性的例子。另外,我应该在每个页面上使用getInıtialProps还是可以使用in
class MyMobxApp extends App {
static async getInitialProps(appContext) {
// Get or Create the store with `undefined` as initialState
// This allows you to set a custom default initialState
const mobxStore = initializeStore()
// Provide the store to getInitialProps of pages
appContext.ctx.mobxStore = mobxStore
let appProps = await App.getInitialProps(appContext)
return {
...appProps,
initialMobxState: mobxStore,
}
}
constructor(props) {
super(props)
const isServer = typeof window === 'undefined'
this.mobxStore = isServer
? props.initialMobxState
: initializeStore(props.initialMobxState)
}
....