• Stars
    star
    155
  • Rank 240,864 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Lift a React component's state into the url

with-url-state

CircleCI codecov Npm

Lifts the state out of a react component and into the url

color-example

Hooks

There is a hook based api available on the 3.0.0 branch, published as a beta on npm.

Installation

To install with npm use

npm install with-url-state --save

To install with yarn use

yarn add with-url-state

Usage

Check out the the demo view the code or play with it in CodeSandbox.

Using javascript

import React from 'react'
import { withUrlState } from 'with-url-state'

const enhance = withUrlState(props => ({ color: 'blue' }))

export const UrlForm = enhance(props => (
  <div className="UrlForm">
    <div className="current-state" style={{ backgroundColor: props.urlState.color }}>
      <div>{props.urlState.color}</div>
    </div>
    <div className="color-buttons">
      <button className="Red" onClick={() => props.setUrlState({ color: 'red' })}>
        Red
      </button>
      <button className="Green" onClick={() => props.setUrlState({ color: 'green' })}>
        Green
      </button>
      <button className="Blue" onClick={() => props.setUrlState({ color: 'blue' })}>
        Blue
      </button>
    </div>
  </div>
))

Using typescript

import React from 'react'
import { withUrlState, UrlStateProps } from 'with-url-state'

type OwnProps = {}
type UrlState = { color: string }

const enhance = withUrlState<UrlState, OwnProps>((prop: OwnProps) => ({ color: 'blue' }))

export const UrlForm = enhance((props: OwnProps & UrlStateProps<UrlState>) => (
  <div className="UrlForm">
    <div className="current-state" style={{ backgroundColor: props.urlState.color }}>
      <div>{props.urlState.color}</div>
    </div>
    <div className="color-buttons">
      <button className="Red" onClick={() => props.setUrlState({ color: 'red' })}>
        Red
      </button>
      <button className="Green" onClick={() => props.setUrlState({ color: 'green' })}>
        Green
      </button>
      <button className="Blue" onClick={() => props.setUrlState({ color: 'blue' })}>
        Blue
      </button>
    </div>
  </div>
))

Using the render-prop component

import React from 'react'
import { UrlState } from 'with-url-state'

type OwnProps = {}
type UrlState = { color: string }

export const UrlForm = (props: OwnProps) => (
  <UrlState
    initialState={{ color: 'green' }}
    render={({ setUrlState, urlState }) => (
      <div className="UrlForm">
        <div className="current-state" style={{ backgroundColor: urlState.color }}>
          <div>{urlState.color}</div>
        </div>
        <div className="color-buttons">
          <button className="Red" onClick={() => setUrlState({ color: 'red' })}>
            Red
          </button>
          <button className="Green" onClick={() => setUrlState({ color: 'green' })}>
            Green
          </button>
          <button className="Blue" onClick={() => setUrlState({ color: 'blue' })}>
            Blue
          </button>
        </div>
      </div>
    )}
  />
)

Motivation

with-url-state automates the query parameter manipulations, simplifying URL sharing for search results, querying data or tracking a visible portion of a map.

The api provided is:

  • based on higer-order-components which makes it composable and testable
  • has a render-prop alternative for convenience
  • type-safe thanks to Typescript
  • very similar to Reacts built in state apis, so converting a component which already manages state is usually as simple as replacing setState with setUrlState!

Pollyfill

For use in IE11 you will need https://github.com/kumarharsh/custom-event-polyfill and add import 'custom-event-polyfill'; if (typeof Event !== 'function') { window.Event = CustomEvent; } to the upper scope of your application.

More Repositories

1

react-native-json-tree

React Native JSON Viewer Component, based on react-json-tree
JavaScript
113
star
2

reason-standard

An easy-to-use, comprehensive, and safe standard library enhancement for Reason and OCaml.
OCaml
53
star
3

jest-to-match-shape-of

A Jest matcher to verify the shape of an object. Makes integration testing simple.
TypeScript
41
star
4

splitwise-node

A javascript wrapper for the Splitwise API.
JavaScript
10
star
5

react-justified-layout

Flickr's justifed layout as a React component
JavaScript
9
star
6

git-watch

Automatically pull non conflicting remotes
CSS
7
star
7

with-notification-system

Create notifications from your react components
TypeScript
6
star
8

higher-order-form

A collection of packages to simplify working with forms
TypeScript
5
star
9

create-reducer-extra

Create boilerplate-free, type-safe Redux reducers
TypeScript
5
star
10

react-native-demo

A demo app to accompany my talk on react native
JavaScript
3
star
11

async

TypeScript
3
star
12

advent-of-code

My Advent of Code solutions
OCaml
2
star
13

reason-luxon

BuckleScript bindings for Luxon
Reason
2
star
14

jumpy-bird-react-demo

Live coding demo.
JavaScript
2
star
15

deanmerchant.com

The repository for deanmerchant.com
JavaScript
2
star
16

lunch

Eat lunch
JavaScript
2
star
17

reason-log-update

Bucklescript bindings to log-update
Reason
1
star
18

jest-to-render-without-error

A Jest matcher to verify the render method of a React component
TypeScript
1
star
19

reason-uuid

Bucklescript bindings for node-uuid
Reason
1
star
20

hypernova-go

A Go client for Hypernova
Go
1
star
21

karma-requirejs-typescript

Example project demonstrating a way to use the karma test runner with an AMD target for typescript.
JavaScript
1
star
22

with-url-state-deprecated

RENAMED this package is now called use-url-state
TypeScript
1
star
23

ppx_fields

OCaml
1
star
24

http-nodejs

JavaScript
1
star
25

pool-ladder

React
JavaScript
1
star
26

static-website

Serve some files or a static website with minimal fuss
JavaScript
1
star
27

better-bleau

CSS
1
star
28

json-web-tokens-spark-kotlin

Kotlin
1
star
29

lava-lights

Uses the rasberry pi's gpio pins to control a lava-lamp based build status indicator
JavaScript
1
star