【问题标题】:Using localStorage with Next.js before componentDidMount在 componentDidMount 之前将 localStorage 与 Next.js 一起使用
【发布时间】:2019-04-14 23:22:33
【问题描述】:

我们正在构建一个应用程序,我们需要在调用 componentDidMount 之前将一些数据存储在 localStorage 中。有没有办法做到这一点?就是存储各种API调用所需的一些设置。

【问题讨论】:

    标签: reactjs next.js


    【解决方案1】:

    根据文档,您的 React 组件的 lifecycle will happen in this order。虽然您可以在构造函数或getDerivedStateFromProps 中触发保存到localStorage,但听起来您可能想等到稍后再进行componentDidMount 中正在进行的API 调用。您可以将您的逻辑链设置为仅在成功写入 localStorage 后触发 API 调用?

    【讨论】:

      猜你喜欢
      • 2016-11-20
      • 2021-09-16
      • 2018-10-04
      • 1970-01-01
      • 2019-02-05
      • 1970-01-01
      • 2017-12-25
      • 2020-09-18
      相关资源
      最近更新 更多