简体   繁体   English

在 Next.js 中制作页面 state 的快照

[英]Make snapshot of page state in Next.js

I faced with some problem while developing Next.js application.我在开发 Next.js 应用程序时遇到了一些问题。 It is rather an architectural issue.这是一个架构问题。

I want to switch between routes, but keeping all the states on the page so that I can return to page without loosing it's state.我想在路线之间切换,但保留页面上的所有状态,以便我可以返回页面而不会丢失它的 state。 I understand that I need to use initalProps at the top level.我知道我需要在顶层使用 initalProps 。 But this is only suitable for simple cases.但这仅适用于简单的情况。 Let's take an example where there're hundreds of states on a page with different levels of hierarchy.让我们举一个例子,一个页面上有数百个具有不同层次结构的状态。 Is it possible to make a snapshot of all page states?是否可以对所有页面状态进行快照?

I look towards memo from React.我看向 React 的备忘录 Also, I think Redux would help me, but I don't use it in the application at all and it's one more dependency.另外,我认为 Redux 会对我有所帮助,但我根本不在应用程序中使用它,它是一种依赖。 Perhaps this can be solved using the Context Api .也许这可以使用Context Api来解决。

If you have global state that you need to share between different pages and the state is simple you can use Context API , the Component needs to be wrapped by the ContextProvider in the custom app component in _app.js .如果您有全局 state 需要在不同页面之间共享,并且 state 很简单,您可以使用Context API ContextProviderComponent需要在自定义_app.js中包装。

The pseudo-code might look something like this for your use case,对于您的用例,伪代码可能看起来像这样,

// context and the provider component

export const MyContext = React.createContext();


export const MyCtxProvider = ({ children }) => {
   
  // state logic  
  
  return (
    <MyContext.Provider value={/* state value */}>
       {children}
    </MyContext.Provider>
  )
}

in _app.js_app.js

import App from 'next/app'
import { MyCtxProvider } from './path-to-myctxprovider'

class MyApp extends App {
  render() {
    const { Component, pageProps } = this.props
    return (
      <MyCtxProvider>
        <Component {...pageProps} />
      </MyCtxProvider>
    )
  }
}

export default MyApp

Here is an example from nextjs repo that uses context API to share state between pages.这是 nextjs 存储库中的一个示例,它使用上下文 API 在页面之间共享 state。

But if your global state is somewhat complex ie you want to keep both server state(fetched data from a remote API) and UI state in the global state, you might wanna use a state management system like redux in this case. But if your global state is somewhat complex ie you want to keep both server state(fetched data from a remote API) and UI state in the global state, you might wanna use a state management system like redux in this case. There are a lot of examples in the nextjs repo showing how to implement it. nextjs repo 中有很多例子展示了如何实现它。 You can start from there.你可以从那里开始。

As far as I understand, it will be an architectual problem.据我了解,这将是一个架构问题。 For global state management you need to use eg: Redux, graphql, ContextAPI , or give a global state to your app in pages/_app.js . For global state management you need to use eg: Redux, graphql, ContextAPI , or give a global state to your app in pages/_app.js . That will wrap your pages, and provide a cross pages state.(You can modify, and reuse that)这将包装您的页面,并提供交叉页面 state。(您可以修改并重用它)
Opnion : Implement Redux , if it"s really need. (for large amount of data in the state). Because it"s easier to implement, then remove it. Opnion : 实现Redux ,如果它真的需要。(对于大量的数据状态)。因为它更容易实现,然后删除它。
Other case: Use the inbuild ContextAPI , or the pages/_app.js state handler till it"s fit for your application.其他情况:使用内置ContextAPIpages/_app.js state 处理程序,直到它适合您的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM