1.1.1 • Published 5 years ago

redux-connected-ui v1.1.1

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

Redux Connected UI

Purpose

Creating new state in Redux, using the standard methods - add an action type, add an action, add a reducer - can take a lot of typing. Sometimes we need local UI state in the redux store. The purpose of this library is to make keeping it there easier, and more specifically, to make connecting to the store closer to the UI that needs to be connected, easier.

Getting started

Install

npm install --save redux-connected-ui or yarn add redux-connected-ui

Note that this library expects your project to include react, react-redux and redux already.

Reducer

// store.js
import { createStore, combineReducers } from "redux";
import { connectedReducer } from "redux-connected-ui";

const rootReducer = combineReducers({
  // redux-connected-ui expects to be at the "ui" key of your store
  // if you need to use a different key, see "Using a different key" below
  ui: connectedReducer,
  ...reducers, // your other reducers
});

const store = createStore(rootReducer);

combineReducers docs

Using the Connected component

// App.js
import React from "react";
import { Provider } from "react-redux";
// note typical redux apps only have one instance of `Provider` near the top of the component tree
// Provider doesn’t need to be added anywhere else for redux-connected-ui
import store from "./store";
import { Connected } from "redux-connected-ui";

const App = () => (
  <Provider store={store}>
    <div>
      <header>
        <h1>Awesome App</h1>
      </header>
      <main>
        <Connected
          namespace="example"
          subscription="boxColor"
          initialValue="red"
        >
          {({ value, updateValue }) => (
            <div
              style={{ height: 20, width: 20, backgroundColor: value }}
              onClick={() => {
                updateValue(value === "red" ? "blue" : "red");
              }}
            />
          )}
        </Connected>
        <Connected
          namespace="example"
          subscription="myTextInput"
          initialValue="wat"
        >
          {({ value, handleInputChange }) => (
            <input value={value} onChange={handleInputChange} name="foo-42" />
          )}
        </Connected>
      </main>
    </div>
  </Provider>
);

In this example, the values from the Connected components will be in the store at state.ui.example.values.

state.ui.example.values.boxColor -> "red"
state.ui.example.values.myTextInput -> "wat"

API

Connected

Props

PropTypeRequiredDefaultDescription
children(renderProps) => React ElementyesundefinedChildren is a function that accepts the argument described below and returns a React element, typically would be JSX. The children function can be declared as nested JSX (as in the example above) or passed explicitly as children to Connected
namespacestringyesundefinedThe namespace in which to read/write the subscribed key/value
subscriptionstringyesundefinedThe key at which to read/write the subscribed value
initialValueanynoundefinedThe initial value for the subcribed key
retainValueOnDestroybooleannofalseRetain the current value when the rendered component unmounts. Value will be cleared by default.

children prop render function argument

PropertyTypeDescription
valueanyThe current value of the component’s subscription
updateValue(newValue: any) => voidFunction accepts a single argument of the new value for the component’s subscription.
updateValues(updateFunction: (currentValues: Object) => newValues: Object) => voidFunction accepts a single argument of a function that receives the current values of the component’s namespace and must return the entire new values for the namespace
resetValue() => voidFunction restores the component’s subscription to its initialValue when invoked
resetValues() => voidFunction restores all values in the component’s namespace to their initialValue
handleInputChange(eventTarget: HtmlInputElement) => voidValue updater to be used specifically for HTML inputs. Updates the component’s subscription to event.target.value.

ConnectAll

Props

PropTypeRequiredDefaultDescription
children(renderProps) => React ElementyesundefinedChildren is a function that accepts the argument described below and returns a React element, typically would be JSX. The children function can be declared as nested JSX (as in the example above) or passed explicitly as children to ConnectAll
namespacestringyesundefinedThe namespace to be subscribed to
initialValuesObjectnoundefinedThe initial values for the subcribed namespace. Note that this should only be used if no Connected component sets any initialValue for the same namespace.
retainValuesOnDestroybooleannofalseRetain the current values when the rendered component unmounts. Values will be cleared by default.

children prop render function argument

PropertyTypeDescription
valuesanyThe current values of the component’s namespace
updateValues(updateFunction: (currentValues: Object) => newValues: Object) => voidFunction accepts a single argument of a function that receives the current values of the component’s namespace and must return the entire new values for the namespace
resetValues() => voidFunction restores all values in the component’s namespace to their initialValue

Using a different key

This library exports createConnected and createConnectAll functions for connecting UI at a store key other than "ui". Usage at "otherUi" is something like this:

// MyConnectedComponents.js
import { createConnected, createConnectAll } from "redux-connected-ui";
const location = "otherUi";
export const ConnectedAtOtherUi = createConnected(location);
export const ConnectAllAtOtherUi = createConnectAll(location);
// store.js
import { createStore, combineReducers } from "redux";
import { connectedReducer } from "redux-connected-ui";

const rootReducer = combineReducers({
  // redux-connected-ui expects to be at the "ui" key of your store
  // if you need to use a different key, see "Using a different key" below
  otherUi: connectedReducer,
  ...reducers, // your other reducers
});

const store = createStore(rootReducer);
1.1.1

5 years ago

1.1.0-beta.0

5 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago