Giter VIP home page Giter VIP logo

react-values's Introduction

A set of tiny, composable React components
for handling state with render props.


Why? · Principles · Examples · Documentation · Contributing!



react-values gives you a set of simple, composable helpers that let you build more complex, stateful UI components like toggles, dropdowns, lists, checkbox groups, popovers, tooltips, you name it!

It does this using a small render-prop-based API that exposes helpful transforms like toggle, increment, filter, etc. depending on the type of value, all based on JavaScripts native value types...

  • Any values provide simple transforms like set and clear.
  • Array values provide native methods like push, pop, filter, etc.
  • Boolean values provide toggle, which we've all re-implemented 100 times.
  • Date values provide really helpful transforms like setHours and incrementMonth.
  • Map values provide native methods like set, delete and clear.
  • Number values provide increment and decrement, which have also been re-written in every codebase ever.
  • Object values provide helpful transforms like set, unset and assign.
  • Set values provide native methods like add, delete and clear.
  • String values provide native methods like concat, repeat, trim, etc.

This saves you from constantly re-writing the same state management logic, so you can keep your components focused on behavior and presentation.

For example, here's the classic state management "counter" example:

import { NumberValue } from 'react-values'

const Counter = () => (
  <NumberValue defaultValue={0}>
    {({ value, increment, decrement }) => (
      <button onClick={() => increment()}>+1</button>
      <span>{value}</span>
      <button onClick={() => decrement()}>-1</button>
    )}
  </NumberValue>
)

Of going further, here's a full fledged <Toggle> (respecting value/defaultValue and providing onChange) implemented in just a few lines of code using a <BooleanValue>:

import { BooleanValue } from 'react-values'

const Toggle = ({ value, defaultValue, onChange }) => (
  <BooleanValue value={value} defaultValue={defaultValue} onChange={onChange}>
    {({ value: on, toggle }) => (
      <Track on={on} onClick={toggle}>
        <Thumb on={on} />
      </Track>
    )}
  </BooleanValue>
)

const Track = styled.div`
  position: relative;
  height: 25px;
  width: 50px;
  background-color: ${props => (props.on ? 'lightgreen' : 'lightgray')};
  border-radius: 50px;
`

const Thumb = styled.div`
  position: absolute;
  left: ${props => (props.on ? '25px' : '0')};
  height: 25px;
  width: 25px;
  background-color: white;
  border-radius: 50px;
`

But you can go further, because react-values can "connect" a single value across multiple components. This is helpful any time you need a "global" piece of state in your app, without wanting to add tons of complexity.

For example, using the <Toggle> from above, here's a modal you can open and close from anywhere in your app:

import { createBooleanValue } from 'react-values'
import { Modal, Toggle } from './ui'

const ModalValue = createBooleanValue(false)

const App = () => (
  <div class="app">
    <div class="sidebar">
      <ModalValue>
        {({ value, set }) => (
          <Toggle value={value} onChange={set} />
        )}
      </ModalValue>
    </div>
    <div class="content">
      <ModalValue>
        {({ value: opened }) => (
          opened && <Modal />
        )}
      </ModalValue>
    </div>
  <div>
)

The primitives react-values gives you seem simple at first, but they can be composed together to create complex behaviors that are still easy to reason about, in just a few lines of code.


Why?

While building an app with React, you end up building a lot of stateful components in the process. Whether at the UI kit level for things like toggles, tooltips, checkbox groups, dropdown, etc. Or at the app level for modals, popovers, sorting, filtering, etc.

In the process, you end up re-implementing run of the mill state handling logic all over the place—whether with this.setState or by adopting some "state management framework" and writing the same boilerplate over and over again. And for your components to be nicely reusable across your application you augment them to handle both "controlled" and "uncontrolled" use cases using value or defaultValue. And to make things a bit more manageable, you re-invent common transforms like open, close, toggle, increment, decrement, etc. in lots of different components. And if you're working with a team, you end up doing all of this in slightly different ways throughout your codebase.

In the end, you're now maintaing a lot more logic than necessary, duplicated in many different places in slightly different ways. It gets harder and harder to understand your app's data flow. All while your app's bundle size grows.

react-values solves all of that with a few principles...


Principles

  1. Leverage render props. It uses a render-prop-based API, exposing its state and a handful of convenient transform functions to you with the flexible function-as-children pattern.

  2. Follow React's conventions. Its components follow React's own naming conventions, using familiar concepts like value/defaultValue. This makes it extremely easy to slot into existing codebases or frameworks.

  3. Follow JavaScript's conventions. It exposes JavaScript's familiar, built-in methods like setDate/setHours, push/pop, filter, concat, etc. to avoid reinventing the wheel and forcing you to constantly read documentation.

  4. Be extremely lightweight. It's extremely lightweight (and tree-shakeable), with most components weighing just a few hundred bytes, so you can even import it from public-facing component libraries.

  5. Prioritize convenience. It's designed to provide convenient functions like increment, toggle, and smarter ones like incrementDate, decrementMonth, so you can build complex interactions in just a few lines of code.


Examples

To get a sense for how you might use react-values, check out a few of the examples:

  • Basic Toggle — using a Boolean to create a simple toggle component.
  • Reusable Toggle — showing how you might turn that toggle into a controlled component in your own UI kit.
  • Counter — a simple counter using a Number and its convenience transforms.
  • Connected Counters — two counters that are connected together, sharing a single value.
  • Time Picker — a more complex time picker component, using Date and its convenience transforms.
  • Filtering — a basic String value used for filtering a list.
  • Checkbox Set — using a Set to keep track of a checkbox group.
  • Simple Tooltip — a simplistic tooltip implemented as a Boolean.
  • Simple Modal — a simplistic modal implemented as a Boolean.
  • Connected Modal — a modal whose opened/closed state is controllable from other components.

If you have an idea for an example that shows a common use case, pull request it!


Documentation

If you're using react-values for the first time, check out the Getting Started guide to familiarize yourself with how it works. Once you've done that, you'll probably want to check out the full API Reference.

If even that's not enough, you can always read the source itself.

There are also translations of the documentation into other languages:

If you're maintaining a translation, feel free to pull request it here!


Contributing!

All contributions are super welcome! Check out the Contributing instructions for more info!

react-values is MIT-licensed.

react-values's People

Contributors

alexcarpenter avatar cdock1029 avatar felippenardi avatar fouad avatar ianstormtaylor avatar jordanthomas avatar mucahit avatar prestaul avatar thekashey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-values's Issues

ArrayValue example

Is there an <ArrayValue /> example I could see where push is done ? My array changes but no re-render happens when I do a push

Change main entry to dist

Hi @ianstormtaylor!
Right now it is not convenient to use this library with babelify/browserify, since that uses ES6 export and requires global browserify transform.
Changing main entry in package.json to dist fixes this problem, also that is well-established practice.

Appreciate your attention.

powerplug

I don't have a horse in the race, but this project seems derivative of react-powerplug. Should perhaps have some attribution? Only seems fair to me

Edit: i could of course be wrong. In which case please ignore :)

add <ObjectValue> helper

This would be similar to <MapValue in terms of the convenience transforms, but for operating on plain objects instead.

Can't call setState (or forceUpdate) on an unmounted component

Using <BooleanValue> to control whether a modal is open, which holds a form to update data rendered in a table rows with react-table. Default rows render with an expander closed, so the BooleanValue isn't rendered.. until the row is expanded.

After an async data update, my parent component re-renders the table..

I'm generically closing the modal with set(false) whether the modal was closed simply with a Cancel, or an actual re-rendering data update.

On re-render the <BooleanValue> is not rendered since row is not expanded.

Of course that generates this error:

Can't call setState (or forceUpdate) on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.
    in BooleanValue

Is this something that should be handled internally within this library? Then we don't have to worry about tracking mounted state or rewrite separate logic for different toggling use cases...

If component is gone, then these mutate operations could be no-ops...

rename `<AnyValue>` to just `<Value>`

Using <AnyValue> feels awkward in most places it's used, since it's usually when you don't care about the type. Which, 90% of the time, is when you happen to be using strings, but more in an "enum" sort of use case—for things like state machines.

Instead it would be nice to just call it <Value>.

Memoize render props

In my experience it is common to pass set from Any down to the components, but the value will be updated on every value change, as long it is creating on every render.

Probably, creating some factory to produce Component with memoized callback is a better way, and it shall not increase the size of this library.

like

const ArrayValue = produce({
        speread: value => value,
        actions: {
           first: value => value.value[0],
           last: value => value.value[Math.max(0, value.value.length - 1)],
           clear: value => () => value.set([]),
           concat: value => proxy(value, 'concat'),
           fill: value => proxy(value, 'fill'),
           filter: value => proxy(value, 'filter'),
           .......
      }
    })
)

add "connected" values

Right now the values are limited to being a single point in the render tree. But it's fairly common to end up needing to use the same value in two places, and have them stay in sync. This can be solved with "context", or with things like Redux, but they're all kind of overkill.

It would be cool if this library exposed factory functions for each type of value, so that you could create your own "connected" values. For example:

import { createBooleanValue } from 'react-values'

const ReadonlyValue = createBooleanValue(false)

<App>
  <Content>
    <ReadonlyValue>
      {({ value, toggle }) => (
	    ...
      )}
    </ReadonlyValue>
  </Content>
  <Sidebar>
    <ReadonlyValue>
      {({ value, toggle }) => (
	    ...
      )}
    </ReadonlyValue>
  </Sidebar>
</App>

It would give you a component that has the same interface as the usual <BooleanValue>-style components, but maintain its state across multiple render locations. And it would also expose the transforms as static methods, for convenience, like ReadonlyValue.toggle().

Pretty sure this would eliminate like 90% of the use cases people end up "needing" to move to a global store like Redux/Mobx for.

add `disabled` prop

I think it would be helpful to have a disabled prop that each of the components respected. When disabled no transforms take effect even when called. Makes it easy to "disable" any component that way without having to check some other state each time you want to transform.

add hooks support

Once hooks reaches the non-beta branches, react-values should expose themselves as hooks. And eventually we can deprecate the render prop approach.

ignore invalid arguments to `increment/decrement/etc.`

Right now, the convenience transforms are "smart" in that if you do:

<NumberValue>
  {({ increment }) => (
    <button onClick={increment} />
  )}
</NumberValue>

It will fail because it's actually being called as increment(event). Instead you have to write it as:

<NumberValue>
  {({ increment }) => (
    <button onClick={() => increment()} />
  )}
</NumberValue>

But this is such a common use case that it might be nice to either ignore Event objects specifically, or just ignore anything that isn't a "valid" value.

It's also easy to forget this, because onClick={toggle} or onClick={clear} works since they don't care about the event argument in the first place.

Looking for mechanism to style based on `disabled` prop

What would be the correct way to style a component based on whether a react-values hoc had the disabled property set? It really looks like you can only access the value but maybe there's nothing wrong with leveraging closure to get at whichever variable is used to set disabled?

Don't mutate "value"

Some operations, like array.sort do mutate original value.
I know, that you are aware of it, but why do you returning muted value, not mutated copy of the value?
Current approach probably could poison render, as long it may change value during the render.

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.