All Projects → anvaka → Query State

anvaka / Query State

Licence: mit
Application state in query string

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Query State

Go Sdk
Dapr SDK for go
Stars: ✭ 149 (+69.32%)
Mutual labels:  state, binding
Samples
Community driven repository for Dapr samples
Stars: ✭ 104 (+18.18%)
Mutual labels:  state, binding
Docker Bind
Bind caching DNS server on Debian with wild-card domain support
Stars: ✭ 50 (-43.18%)
Mutual labels:  binding
Raylib Lua
A simple and easy-to-use Lua library to enjoy videogames programming
Stars: ✭ 80 (-9.09%)
Mutual labels:  binding
Proteus
Proteus : A JSON based LayoutInflater for Android
Stars: ✭ 1,179 (+1239.77%)
Mutual labels:  binding
Alveron
Elm & Reason inspired state management for React
Stars: ✭ 57 (-35.23%)
Mutual labels:  state
Vuex Multi Tab State
💾🔗🖥️ Share, synchronize and persist state between multiple tabs with this plugin for Vuex. TypeScript types included.
Stars: ✭ 77 (-12.5%)
Mutual labels:  state
Scala Js Binding
ScalaJS html binding library
Stars: ✭ 48 (-45.45%)
Mutual labels:  binding
Alfa
Effortless React State Management.
Stars: ✭ 86 (-2.27%)
Mutual labels:  state
React Composition Api
🎨 Simple React state management. Made with @vue/reactivity and ❤️.
Stars: ✭ 67 (-23.86%)
Mutual labels:  state
Expostal
Elixir binding for Libpostal - a library for parsing/normalizing street addresses around the world. Powered by statistical NLP and open geo data.
Stars: ✭ 80 (-9.09%)
Mutual labels:  binding
Countries States Cities Database
🌍 World countries, states, regions, provinces, cities, towns in JSON, SQL, XML, PLIST, YAML, and CSV. All Countries, States, Cities with ISO2, ISO3, Country Code, Phone Code, Capital, Native Language, Timezones, Latitude, Longitude, Region, Subregion, Flag Emoji, and Currency. #countries #states #cities
Stars: ✭ 1,130 (+1184.09%)
Mutual labels:  state
Yewdux
Redux-like state containers for Yew apps
Stars: ✭ 58 (-34.09%)
Mutual labels:  state
Laravel Country State
A helper to list countries & states in English in Laravel 5.1+
Stars: ✭ 77 (-12.5%)
Mutual labels:  state
Svelte Store Router
Store-based router for Svelte
Stars: ✭ 54 (-38.64%)
Mutual labels:  state
React Inform
Simple controlled forms with validations in react
Stars: ✭ 81 (-7.95%)
Mutual labels:  state
Naija State Local Government
A simple utility library that lists Nigeria states and local governments with zero dependency
Stars: ✭ 50 (-43.18%)
Mutual labels:  state
Slacko
A neat interface for Slack
Stars: ✭ 64 (-27.27%)
Mutual labels:  binding
React Easy Params
🔗 Auto synchronize your state with the URL and LocalStorage.
Stars: ✭ 73 (-17.05%)
Mutual labels:  state
Statelayout
StateLayout is a simple-use Android layout library which handles Loading, Content and Error states
Stars: ✭ 88 (+0%)
Mutual labels:  state

query-state Build Status

Application state in query string.

demo

https://anvaka.github.io/query-state/#?name=world

usage

Grab it from npm and use with your favorite bundler:

npm install query-state --save

Or download from CDN:

<script src='https://cdn.rawgit.com/anvaka/query-state/v4.0.0/dist/query-state.min.js'></script>

If you downloaded from CDN the library will be available under queryState global name.

// create a new query state instance
var qs = queryState();

// get current application state from the hash string:
var appState = qs.get();

// you can also monitor for changes in the query string:
qs.onChange(function(appState) {
  // prints new application state on each hash update
  console.log('app state changed!', appState);
});

// If you want to set a new value in the app state:
qs.set('answer', 42);

// Now the query string will have `answer=42` part in it.
console.log(window.location.hash.indexOf('answer=42')) // prints value > 0.

// You can also set multiple values at once:
qs.set({
  name: 'Haddaway',
  song: 'What is love?'
});

// NOTE: The call above merges new properties. It appends two new properties to 
// the current query string

defaults

If you want to initialize app state with default values, you can pass them into query state function:

// this will set query string to `answer=42`, unless it already has key called
// "answer". In which case query string's value will take precedence.
var qs = queryState({
  answer: 42
});

type limitations

This is a very simple module that currently does not support nested objects. I.e. you cannot set application state to {foo: {bar: 42}}. If you need this behavior, most likely this module is not for you.

We do support primitive types serialization/deserialization:

  • Numbers
  • Dates
  • Strings

Sharing between modules (singleton)

If you are using a bundler (e.g. browserify or webpack), its often desirable to have just one instance of the application state, shared between files. Normally this is accomplished via singleton pattern. The library exposes singleton instance for your convenience:

// fileA.js
var stateInFileA = queryState.instance();

// fileB.js
var stateInFileB = queryState.instance();

// Both `stateInFileA` and `stateInFileB` are the same:
// assert(stateInFileA === stateInFileB); // this is true!

NOTE: This is "lazy" implementation of the singleton: The instance is not created until you call queryState.instance(). After initial call the return value is always the same.

Defaults in singleton scenario

Each of your module may desire to pass its own defaults. You can do it by passing optional defaults object:

// fileA.js
var stateInFileA = queryState.instance({name: 'John'});
// The query string now has `name=John` part.

// fileB.js
var stateInFileB = queryState.instance({age: 42});
// Now query string has both parts: `name=John&age=42`

// fileC.js
// Note: singleton instance never overwrites query string values. So if someone
// sets argument that already exists, the library will ignore it:
var stateInFileC = queryState.instance({age: 100, height: 180})

// The query string still has age 42, not 100:
// name=John&age=42&height=180

search instead of hash

By default query-state saves state in the hash part of the url. But what if we want to store state in the search part instead?

Instead of awesome-site.com/#?answer=42 we want awesome-site.com/?answer=42.

Pass an optional argument to tell query-state use the search part:

var qs = queryState({
  answer: 42
}, {
  useSearch: true
});

This can be especially useful if you are planning to share links on social media, as hash part of the query string is not visible to the servers, and they often consider two different links to be the same.

clean up

Normally your app state will live as long as your application. However if you do need to clean up resources (e.g. unloading your app) you can call qs.dispose()

var qs = queryState();

// use it...

// and clean up when you need it:
qs.dispose();

license

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].