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-rewired

v3.0.3

Published

Wire your react app as easy as possible

Downloads

362

Readme

GitHub license npm package Travis Coverage Status styled with prettier

react-rewired

Wire your react app as easy as possible...

  • A fast and easy alternative for react-redux
  • Feels more like react (using internal react state as base model)
  • TypeScript support included
  • flow support included
  • very small package size (v3.0.2 -> 836 B gzipped)
  • high performance (performance play ground in comparison to [email protected] is available here)

Introduction

react-rewired intends to reduce the overhead and complexity when internal react component state is not enough, because you want to store everywhere accessible data which can safely be accessed by your react components and triggers automatically updates, when the data changes.

This should be familiar to any react developer. And the more popular solution for this purpose is the usage of react-redux. But react-redux comes not only with nice dev tools, good documentation, large user base and a consistent philosophy for its usage and idea. In my opinion, I am still looking to often into the dev tools to find out what seems to be broken and my application grows very fast too large with all the overhead, that comes with actions and reducers. Also it is always quite an act to implement flow on top of react-redux.

How to - an example

First of all you need to initialize your Store with some defaults.

import { Wired } from 'react-rewired';

const Store = Wired.store({
    num: 12,
    data: { keys: [] },
    foo: null
})

You will also have to define the types for your State because the breaking change since flow reached version 0.85 requires to define any types in value position. Now let's e.g. define the array on data.keys to contain only strings.

type DataState = { keys: string[] };
type State = {
    num: number,
    data: DataState,
    foo: string | null
};

const data: DataState = { keys: [] }; // you can directly type cast
const Store = Wired.store<State>({
    num: 12,
    data,
    foo: null
})
  • HINT: Types in value position are still very new, but every flow-parser is able to handle those. If you run in troubles with those, you should maybe consider to update your dev dependencies.

The next step is the wrapping of your react tree with the context provider to be able to access the data within any component of the subtree.

const root = document.getElementById('root');
root &&
    ReactDOM.render(
        <Store.root>
            <App />
        </Store.root>,
        root
    );

To access the data within your component you want to wrap it into the corresponding consumer. (HINT: You should not define react components inline but give it a name, because the react dev tools uses that name as default display name. E.g. in the following snippet "MyComponent" will be displayed inside the react dev tools)

type MyComponentStoreProps = {| key?: string, odd: boolean |};
type MyComponentOwnProps = {||};
type MyComponentProps = {| ...MyComponentOwnProps, ...MyComponentStoreProps |};

const MyComponent = ({ key, odd }: MyComponentProps) => <JSX />
// and not wire it
const MyWiredComponent = Store.wire<MyComponentStoreProps, MyComponentOwnProps>(
    MyComponent,
    state => ({
        key: state.data.keys[0],
        odd: state.num % 2 === 1
    })
);
  • HINT: I want to stress out that your application will benefit a lot if you follow the above pattern everywhere. Also I want to strongly encourage you, to use always (if possible) exact prop types, to ensure that no arbitrary props will be provided where not expected.

The last thing you have to know are state updates. There are two different ways you can perform those updates. Just like setState on internal react state.

// simply return those keys you want to update with the new data
// they will be merged and component updates will only be propagated
// to those components who listen to those data points
Store.set({ num: 13, foo: 'bar' });
// MyWiredComponent would update after this change
// because the value of "odd" changed from "false" to "true"

// return a function which will be called with the current state
Store.set(state => ({ num: 3 * state.num })); // num = 3 * 13 = 39
// MyWiredComponent would NOT update after this change
// because the values of "odd" and "key" did not change

If you want to access the data directly e.g. outside any component, you can do the following:

const state = Store.get();
  • ATTENTION: Modifications to the returned state can effect your application in an unexpected way. Even if you know what you're doing, I do not recommend it.

  • To make testing easy even with the context API, you should insert a little snippet into your setupTests.js (see jest option "setupFilesAfterEnv")

Store.set = (Component, mapStateToProps) => {
    const result = props => <Component {...mapStateToProps(Store.get())} {...props} />;
    result.displayName = `Wired(${Component.displayName || Component.name || ''})`;
    return result;
}