GithubHelp home page GithubHelp logo

koddr / storeon-sessionstorage Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 0.0 3.45 MB

Module for storeon to persist data from state to session storage

License: MIT License

JavaScript 91.45% HTML 8.55%
session sessionstorage storeon store-management

storeon-sessionstorage's Introduction

Storeon sessionStorage

Storeon logo by Anton Lovchikov

Tiny module for Storeon to store and sync state to sessionStorage. It restores state from sessionStorage during page loading and saves state on every change.

The methods are the same of localStorage, but there are significant limitations:

  • sessionStorage exists only within current browser tab.
    • Another tab with the same page will have a different storage.
    • But it's shared between iframe on the same tab (provided that they are from the same source).
  • Data continues to exist after reloading the page, but not after closing/opening browser tab.

It is just 172 bytes module (it uses Size Limit to control the size) without any dependencies.

Please note: this module is a fork of localStorage by Ivan Menshykov. Because of this, init version of module is v0.6.3 (latest version of @storeon/localstorage when fork).

Installation

npm install --save storeon-sessionstorage

Usage

If you want to store and sync state to sessionStorage you should import the persistState from storeon-sessionstorage and add this module to createStore.

import createStore from 'storeon'
import persistState from 'storeon-sessionstorage'

let name = store => {
  store.on('@init', () => ({ name: '' }))
  store.on('save', (state, name) => ({ name: name }))
};

const store = createStore([name, persistState(['name'])])

Here you can see that the form ask user the name and after that show this name.

import useStoreon from 'storeon/react' // or storeon/preact for Preact
import StoreContext from 'storeon/react/context' // or storeon/preact/context for Preact

const Form = () => {
  const { dispatch, name } = useStoreon('name')

  return (
    <React.Fragment>
      {name !== '' && <h3>Hello {name}!</h3>}
      {name === '' && (
        <div>
          <label>Name</label>
          <input type='text' id='name' />
          <br />
          <button
            onClick={() =>
              dispatch('save', document.getElementById('name').value)
            }
          >
            Save
          </button>
        </div>
      )}
    </React.Fragment>
  )
}

Event after refresh the page the state is updating from sessionStorage. And user see the greeting message.

Example of store state to local storage

persistState(paths, config)

type paths = Void | Array<String>

If no pass the paths value then persistState store in local storage all state.

type config.key = String

Default value of config.key is storeon. This key is using to store data in local storage.

Acknowledgments

This module based on @storeon/localstorage.

LICENSE

MIT

storeon-sessionstorage's People

Contributors

dependabot-preview[bot] avatar koddr avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

storeon-sessionstorage's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.