Code Monkey home page Code Monkey logo

react-qparams's Introduction

react-qparams

One of the hardest parts of working with a SPA is keeping the applications internal state in sync with the URL bar. Conceptually, React follows this formula:

f(state) := view

But for SPA's it's closer to:

f(state, url) := view

However, it's difficult to keep the URL bar in sync with your internal state. There ends up being a lot of calls to history.push and withRouter in order to get the history instance. That is, at the core, what this module is for.

Installation

$ yarn add react-qparams query-string@5

Additionally, you need react, react-dom, prop-types, and react-router-dom, but you probably already have those.

QueryParams

import { QueryParams } from "react-qparams";

function RenderView(props) {
  return (
    <QueryParams>
      {query => (
        <button onClick={() => query.setQuery({ tab: "other" })}>
          {query.tab || "default"}
        </button>
      )}
    </QueryParams>
  );
}

You can also add a replace option if you want setQuery to replace the last location in the history.

import { QueryParams } from "react-qparams";

function RenderView(props) {
  return (
    <QueryParams>
      {query => (
        <button
          onClick={() => query.setQuery({ tab: "other" }, { replace: true })}
        >
          {query.tab || "default"}
        </button>
      )}
    </QueryParams>
  );
}

Props

children or render

A render prop. Both children and render receive the same arguments:

  • An object with all the query params in the url bar, along with
  • A setQuery function that mutates the URL bar, and takes an object like setState

QueryLink

import { QueryLink } from "react-qparams";

function RenderView(props) {
  return <QueryLink query={{ name: "value" }}>Go Here!</QueryLink>;
}

Props

All react-router-dom Link props are supported, as well as:

query

An object that will be merged with the current query string when the link is clicked.

react-qparams's People

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

react-qparams's Issues

QueryNavLink component

This one is a little bit more complicated, because query-string allows for arrays of values. That said, it only allows for arrays of primitives like numbers and strings, and doesn't support arrays of arrays.

With that in mind, it should be possible to compare the given query params with the query params in the url, and apply the active class if needed

Remove default export

Now that we have both QueryParams and QueryLink, it would make sense to remove the default export.

This is a simple change, but it's a breaking one that I want to remove before 1.0

QueryLink component

Now that we can set the values in the url programmatically, it would be good to be able to have links that incorporate a query prop.

Ex:

// url: /foo?a=b
<QueryLink query={{ b: 'c' }} > Children </QueryLink>

Would render:

<a href="/foo?a=b&b=c"> Children </a>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.