npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-use-dux

v3.0.6

Published

React bindings for Redux using the new React hooks proposal.

Downloads

20

Readme

react-use-dux

React bindings for Redux using the new React hooks proposal.

NPM JavaScript Style Guide

Table of Contents

Installation

# YARN
yarn add react-use-dux
# NPM
npm install --save react-use-dux

Usage

NOTE: React hooks currently require react and react-dom version 16.7.0-alpha.0 or higher.

In order to use the hooks, your Redux store must be available in the React context from ReduxContext.Provider.

ReduxContext and reduxBatchUpdateMiddleware

Before you can use the hook, you must provide your Redux store via ReduxContext.Provider. Additionally it is recommended that you apply the supplied reduxBatchUpdateMiddleware to your store in order to guarantee that actions that may cause cascading re-renders will be batched into one render. This is due to the way that react-use-dux uses the built in state hook under the hood. It is envisioned that react will prevent these setState cascading updates in the future so hopefully this feature can be removed in a later release.

import {createStore, compose, applyMiddleware} from 'redux';
import {ReduxContext, reduxBatchUpdateMiddleware} from 'react-use-dux';

const store = createStore(/*...*/,compose(applyMiddleware(/*...*/, reduxBatchUpdateMiddleware)));

ReactDOM.render(
  <ReduxContext.Provider value={store}>
    <App />
  </ReduxContext.Provider>,
  document.getElementById('root'),
);

useReduxState(selector, memoArray)

Runs the given selector function to subscribe to a part of the redux state. Preferably call it for each part of the state to which you subscribe. You may subscribe to several portions of the state using a single selector (not recommended). Shallow comparison is now supported by default but can be turned off. This option will be removed in a later version as it will probably cause issues.

Your selector will be memoized using the provided memoArray argument. Ensure that all values captured by the selector function are included in the memoArray.

It is also possible to subscribe to the entire state by passing void arguments but this is not recommended.

Avoid creating deeply nested state subscriptions as this will cause the state subscription to fire for every action as no stronger/deeper form of comparison is supported for performance reasons.

import {useReduxState} from 'react-use-dux';

const MyComponent = (props) => {

    const x = useReduxState(state => state.x);
    const y = useReduxState(state => state.y);
    //OR using shallow comparison
    const { x, y } = useReduxState(({ x, y }) => ({ x, y }), [], true);

    return (
        {\* ...JSX... *\}
    );
};

Avoid capture of subscribed state values in the selectors of subsequent subscriptions as this will cause anomalous behaviour.

//Avoid...
const filter = useReduxState(state => state.filter);
const filteredTodos = useReduxState(state => state.todos.filter(filter), [filter]); //fail sauce.
//Rather...
const filteredTodos = useReduxState(state => state.todos.filter(state.filter));

If you really want to reuse selectors think about adopting a library like reselect. If you look at the provided example application you'll see that it includes the use of reselect as well as argument currying to make it more clear when values are captured and should be added to the memoArray.

useReduxDispatch(dispatcherFactory, memoArray)

Either returns the dispatch method, or returns the result of executing the provided dispatcher factory method.

The output of this method will be memoized using the provided memoArray argument. Ensure that all values captured in your provided function are included in the memoArray.

import {useReduxDispatch} from 'react-use-dux';

const MyComponent = (props) => {

    const dispatch = useReduxDispatch();
    //OR
    const dispatcher = useReduxDispatch(dispatch => { /* create dispatcher */ });
    
    return (
        {\* ...JSX... *\}
    );
};

useReduxBindActionCreators(actionCreator, memoArray)

Either gives an action creator function dispatch, or gives an object (preferred) with action creator function properties dispatch.

The output of this method will be memoized using the provided memoArray argument. Ensure that all values captured in your provided function(s) are included in the memoArray.

import {useReduxBindActionCreators} from 'react-use-dux';
import actions from './dux/actions';

const MyComponent = (props) => {

    const doThing1 = useReduxBindActionCreators(actions.doThing1);
    //OR
    const { doThing1, doThing2 } = useReduxBindActionCreators(actions);


    return (
        {\* ...JSX... *\}
    );
};

Example

To run the example project locally:

# In one terminal, run `yarn start` in the root to rebuild the library itself
cd ./react-use-dux
yarn start

# In another terminal, run `yarn start` in the `example` folder
cd example
yarn start

Changes

This API has been evolving rapidly as I've experimented with it and measured the performance and usability of various design choices. It is more stable now and you can adopt it (please no prod deployment until the react team releases hooks!) without fear of breaking changes for now.

That said if you feel strongly about geting it changed (or heaven forfen you've found a bug) you're welcome to raise an issue or pull request.

Thanks

This project was boostrapped using create-react-library. The code was inspired by the project redux-react-hook but with a few improvements. Thanks also to @ianobermiller for providing some great feedback leading to some great improvements in version 2.

License

MIT © richardpj