# state-as-querystring

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

Latest version **1.2.0** (published 2019-02-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install state-as-querystring
pnpm add state-as-querystring
yarn add state-as-querystring
bun add state-as-querystring
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2019-02-05 |
| First published | 2018-03-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Pavel Zastavnitskiy |
| Maintainers | zastavnitskiy |
| Keywords | redux, state, react, router, react-router, redux-router |

## Links

- npm: https://www.npmjs.com/package/state-as-querystring
- npm.io page: https://npm.io/package/state-as-querystring

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.2.0 (latest) — 2019-02-05
- 1.1.6 — 2018-12-20
- 1.1.5 — 2018-03-19
- 1.1.4 — 2018-03-19
- 1.1.3 — 2018-03-16
- 1.1.2 — 2018-03-16
- 1.1.1 — 2018-03-16
- 1.1.0 — 2018-03-16
- 1.0.5 — 2018-03-15
- 1.0.4 — 2018-03-15
- 1.0.3 — 2018-03-15
- 1.0.2 — 2018-03-15
- 1.0.1 — 2018-03-15
- 1.0.0 — 2018-03-15

## README

# state-as-querystring [![Build Status](https://travis-ci.org/zastavnitskiy/state-as-querystring.svg?branch=master)](https://travis-ci.org/zastavnitskiy/state-as-querystring)
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](https://13n3pr91nl.codesandbox.io/?text=Edit%20me)

### React example
```javascript

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](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/13n3pr91nl)

## Redux example
```javascript
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](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/jlxz5vm5n3)

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](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/mo573kq3rx)

---
_Source: https://npm.io/package/state-as-querystring · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
