Local storage: Transfer data from one page to another


I am trying to transfer local storage (capacitor) data from ‘home’ page to ‘stats’ page.

Do I need to use input/output while transferring data from one page to another one?

How can I do it?


Here are some rules that I’ve developed over the years, that help me avoid pitfalls:

  • Only use device storage for communicating between runs of the app. Don’t use it to pass data between parts of the running app.
  • Never read from device storage more than once per run.
  • Only interact with device storage from services, never pages.

I think what you want here is a service provider that is injected by both pages, and serves as the single source of truth.

1 Like

Thank you very much.

I am a developing a communication app. It will count characters on homepage and show it on stats page.

Do you think local storage can do this?

It can, but I’m not convinced it should. I don’t know what you mean by “homepage”, “characters”, or “count”, but the one question you should be asking yourself is “do I need this information to persist to the next time the app is started up, or can it be recalculated easily?”. If the answer is “yes, I really need it the next time the app is run”, then yes use local storage. If it isn’t, don’t.

Either way, pages shouldn’t know about any of this. Abstract it all out into a single service provider, at which point if your needs change, there’s only one place to edit.

1 Like

Got it.

I need this information to be available next time the app is running.