Skip to content

Save application state in the querystring. Restore it when page is refreshed.

Notifications You must be signed in to change notification settings

zastavnitskiy/state-as-querystring

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

state-as-querystring Build Status

Save your application state in query string between page views.

String, Number and Boolean and Array values will be saved as is, while object values will be encoded as JSON.

Usage

Idea is simple. When state in initiated, we read it from the query string. Whenever it changes, we write it to the query string. This keeps start between page views and allows sharing urls of a given state.

Try here

React example

import { saveStateToURL, readStateFromURL } from "state-as-querystring";

class App extends React.Component {
  // read state from url when initializing the store
  constructor(props) {
    super(props);
    this.state = readStateFromURL();
  }
  
  //save state to url when state is changed
  componentDidUpdate() {
    saveStateToURL(this.state);
  }

  render() {
    return (
        <textarea
          value={this.state.text}
        />
    );
  }
}

Edit 13n3pr91nl

Redux example

import { saveStateToURL, readStateFromURL } from "state-as-querystring";

// read state from url when initializing the store
const store = createStore(reducer, readStateFromURL());

// save state to url when state changes 
store.subscribe(() => saveStateToURL(store.getState()));

Edit redux-state-as-querystring

A bit more complex example - with arrays in the state + partial syncing of the state.

Edit redux-state-as-querystring — a bit more complex example

About

Save application state in the querystring. Restore it when page is refreshed.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published