javascript - Save multiple results in the localstorage
问题描述
I need to save one simulations datas (or more) in localstorage
notary props = datas
Input data example (1 simulation) :
[{"price":"150000","postalCode":"33000","date":"16/12/2019","type":"immobilier"}]
Input multiples datas example (2 or more simulations) :
[{"price":"150000","postalCode":"33000","date":"16/12/2019","type":"immobilier"}, {"price":"250000","postalCode":"75001","date":"16/12/2019","type":"area"}]
if the user does a single simulation i want to save in my localstorage the first example (array of one object)
But if the user does several simulations I want to save second example (array of multiples objects)
To do this you must save a history (the previous values)
componentDidUpdate() {
const { notary } = this.props // datas
const { send } = this.state
const prevResult = localStorage.getItem('notary') // check prev results
let arrayResult = [] // array
if (prevResult !== null) arrayResult = JSON.parse(prevResult)
if (notary !== null && send) {
arrayResult.push({ date: moment().format('YYYY-MM-DD HH:mm:ss'), ...notary })
localStorage.setItem('notary', JSON.stringify(arrayResult))
this.setState({ send: false })
}
}
I tried to retrieve the previous values from localstorage and push new values in a table but it does not work.
In my case an array of several objects is well created but the objects are identical while the results are different
OUTPUT : Here is what I would like to have in localStorage
If the user make only 1 simulation : Array of one object
[{"price":"150000","postalCode":"33000","date":"16/12/2019","type":"immobilier"}]
If the user makes multiples simulations : Array of multiples objects
[{"price":"150000","postalCode":"33000","date":"16/12/2019","type":"immobilier"}, {"price":"250000","postalCode":"75001","date":"16/12/2019","type":"area"}]
Thanks for your help
解决方案
在您的 utils 文件夹中创建一个 localStorager.js,然后将您的参数发送到该 localStorager 函数。
localStorager.js
export const localStorager= state => {
localStorage.clear();
if (state.length > 1) {
//this condition is for multiSimulation
state.forEach((e, i) => {
const serializedState = JSON.stringify(e);
localStorage.setItem(`state${i}`, serializedState);
});
} else {
// this else is for singleSimulation
const serializedState = JSON.stringify(state);
localStorage.setItem(`state`, serializedState);
}
};
然后在您想要处理 localStorage 的组件中将数据传递给您之前创建的 localStorager.js 函数。
您从 componentDidUpdate 使用的组件
import { localStorager} from "../utils/localStorager";
...
//by the way you can use from localStorager in every function that you want.
componentDidUpdate(){
//forExsample
let singleSimulation= [
{
price: "150000",
postalCode: "33000",
date: "16/12/2019",
type: "immobilier"
}
];
let multiSimulation= [
{
price: "150000",
postalCode: "33000",
date: "16/12/2019",
type: "immobilier"
},
{ price: "250000", postalCode: "75001", date: "16/12/2019", type: "area" }
]
}
//and after that use from localStorager and pass these data to that
localStorager(singleSimulation);
localStorager(multiSimulation);
//in this case you should use just pass your notary data
const { notary } = this.props // datas
localStorager(notary)
//this is end of componentDidUpdate
}
推荐阅读
- ruby-on-rails - 使用 React 和 Selenium-Webdriver 和 Rails 作为后端的前端测试
- python - 在 Pandas 中将 DatetimeIndex 转换为 datetime.date
- java - 链表Java添加元素
- ios - 未检测到 SDL2 iOS 多点触控第二个手指向下
- python - Flask Make Azure Blueprint - 得到一个意外的参数“租户”
- python-3.x - 使用 Raspberry Pi 3 和 Python3,我的压力传感器无法输出全部值
- javascript - 如何仅使用 javascript 修剪 url 的查询部分?
- java - java 类路径不像记录的那样工作
- python - 如何在前台打开 IE 实例作为 vagrant 配置
- c++ - Visual Studio Code C++:未找到 unordered_map