bredart
bredart

Reputation: 318

Electron save user changes on page

I have a problem with my electron app. I navigate throw pages by using window.loadUrl(). On some pages, the user needs to enter some data to be saved. The problem is that when the user moves to another page (after he added some data to inputs and etc) and after goes back no changes are detected and electron just loads HTML file with no user data. How it's possible to store this data and give user page with his previously added data? Thanks

Upvotes: 0

Views: 1174

Answers (1)

Get Off My Lawn
Get Off My Lawn

Reputation: 36309

For a local solution, you can store the data within a few data types:

  • localStorage – Persistent from page to page and after the browser closes. Is only removed upon user request according to W3 spec.
  • sessionStorage – Deleted after the tab/page is closed or upon user request according to W3 spec.

From there you can load data as you load/unload pages.

// Save the data
localStorage.setItem('userData', JSON.stringify(data));

// Load the data
let data = JSON.parse(localStorage.getItem('userData') || {});

Note: This only works in the rendering process and not the main process.


For a remote solution, you will need to send the data to a web server where it will process and save the data to in a remote location. An API is a good way to send this data.

Send/Request the data with a unique identifier, such as a userid.

// Save the data
fetch('http://my-website.com/save/123456', { // 123456 = userid
  method: 'post',
  body: JSON.stringify(data)
})

// Get the data
fetch('http://my-website.com/load/123456')
  .then(resp => resp.json())
  .then(json => { data = json })

Upvotes: 1

Related Questions