简体   繁体   English

在componentDidMount之前使用localStorage和Next.js

[英]Using localStorage with Next.js before componentDidMount

We're building an application where we need to store some data in localStorage before componentDidMount is invoked. 我们正在构建一个应用程序,我们需要在调用componentDidMount之前在localStorage中存储一些数据。 Is there a way of doing this? 有办法做到这一点吗? It's to store some settings required for various API calls. 这是存储各种API调用所需的一些设置。

According to the docs, your React component's lifecycle will happen in this order . 根据文档,您的React组件的生命周期将按此顺序发生 While you could trigger the save to localStorage in in the constructor, or in getDerivedStateFromProps , it sounds like you might want to wait to make the API calls that are being made in componentDidMount until later. 虽然您可以在构造函数或getDerivedStateFromProps触发对localStorage的保存,但听起来您可能希望等到稍后在componentDidMount进行API调用。 You could set up your chain of logic to only fire the API call once localStorage has been written to successfully? 您可以将逻辑链设置为仅在localStorage成功写入后触发API调用?

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

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