0

I have an array of objects on a page that I want to have on another page in electron. Both pages have there own javascript file and there is one function.js file.

file1.js

const { createArray, getArray } = require ('./function')

window.onload = () => {
    createArray()
    sessionStorage.setItem("Array", getArray)
    console.log(sessionStorage.getItem("Array")   //[object Object],[object Object],[object Object]
    console.log(getArray) // [{"1": 10}, {"2": 11}, {"3": 12}]
}

By the documentation of Electron getting variables from one page to the other is best done via the HTML 5 API's.

But I'm not able to store an array of object in a sessionStorage. According to this post that is perfectly normale. The solution used here is to stringify. But that is a rather expensive process that I would like to avoid.

Does anybody have any idea how best to share an array of objects between two pages?

1
  • I don't think other solutions would be "less expensive" unless we're talking like huge arrays here. In that case a server side database would probably be preferred. With only 3 objects inside the array, I would opt for localStorange or the IPC system your link to the documentation mentions. Commented Jun 7, 2019 at 11:29

2 Answers 2

0

I would save the array in your background script and provide it to the BrowserWindows using the messaging API: Link

Sign up to request clarification or add additional context in comments.

Comments

0

I think you can solve this problem by simply re use getArray from the other page, so just re import it.

const { createArray, getArray } = require ('./function')

Otherwise you need to use JSON.stringify and JSON.parse using sessionStorage, example:

sessionStorage.setItem("Array", JSON.stringify(getArray));
const data = JSON.parse(sessionStorage.getItem("Array"))

Or you can use IPC to Set the Global's Value.

As side note: Use a more descriptive name instead of "Array" for sessionStorage

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.