All Projects β†’ Dean177 β†’ Use Url State

Dean177 / Use Url State

Licence: mit
Lift a React component's state into the url

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Use Url State

Formik
Build forms in React, without the tears 😭
Stars: ✭ 29,047 (+18761.69%)
Mutual labels:  hooks, higher-order-component, render-props
Flagged
Feature Flags for React made easy with hooks, HOC and Render Props
Stars: ✭ 97 (-37.01%)
Mutual labels:  hooks, higher-order-component, render-props
React Nprogress
βŒ›οΈ A React primitive for building slim progress bars.
Stars: ✭ 173 (+12.34%)
Mutual labels:  hooks, higher-order-component, render-props
Pullstate
Simple state stores using immer and React hooks - re-use parts of your state by pulling it anywhere you like!
Stars: ✭ 683 (+343.51%)
Mutual labels:  hooks, state-management
Redhooks
Predictable state container for React apps written using Hooks
Stars: ✭ 149 (-3.25%)
Mutual labels:  hooks, state-management
Easy Peasy
Vegetarian friendly state for React
Stars: ✭ 4,525 (+2838.31%)
Mutual labels:  hooks, state-management
Use Global Context
A new way to use β€œuseContext” better
Stars: ✭ 34 (-77.92%)
Mutual labels:  hooks, state-management
React Values
A set of tiny React components for handling state with render props.
Stars: ✭ 1,025 (+565.58%)
Mutual labels:  state-management, render-props
React Context Hook
A React.js global state manager with Hooks
Stars: ✭ 50 (-67.53%)
Mutual labels:  hooks, state-management
React Workshop
βš’ 🚧 This is a workshop for learning how to build React Applications
Stars: ✭ 114 (-25.97%)
Mutual labels:  higher-order-component, state-management
Use Substate
πŸ™ Lightweight (<600B minified + gzipped) React Hook to subscribe to a subset of your single app state.
Stars: ✭ 97 (-37.01%)
Mutual labels:  hooks, state-management
React Goodbye
A save reminder component for react router v4+.
Stars: ✭ 120 (-22.08%)
Mutual labels:  higher-order-component, render-props
Constate
React Context + State
Stars: ✭ 3,519 (+2185.06%)
Mutual labels:  hooks, state-management
Radioactive State
☒ Make Your React App Truly Reactive!
Stars: ✭ 273 (+77.27%)
Mutual labels:  hooks, state-management
React Hooks
Essential set of React Hooks for convenient Web API consumption and state management.
Stars: ✭ 515 (+234.42%)
Mutual labels:  hooks, state-management
hook-into-props
Tiny HoC to use React hooks with class components.
Stars: ✭ 44 (-71.43%)
Mutual labels:  hooks, higher-order-component
atomic-state
A decentralized state management library for React
Stars: ✭ 54 (-64.94%)
Mutual labels:  hooks, state-management
micro-observables
A simple Observable library that can be used for easy state management in React applications.
Stars: ✭ 78 (-49.35%)
Mutual labels:  hooks, state-management
zoov
Use 🐻 Zustand with Module-like api
Stars: ✭ 24 (-84.42%)
Mutual labels:  hooks, state-management
Pure Store
A tiny immutable store with type safety.
Stars: ✭ 133 (-13.64%)
Mutual labels:  hooks, state-management

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.

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].