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

v1.0.3

Published

A single store, single action, global state management system for React apps.

Downloads

23

Readme

react-one

A single store, single action, global state management system for React apps.

installation

npm install react-one --save

or

yarn add react-one

Usage

import { Provider, connect } from 'react-one';

Wrap the root of your app with the Provider component and pass it an initialState object.

const INITIAL_STATE = { num: 0 };
...
render() {
  return (
    <Provider
      initialState={INITIAL_STATE}
      onSetState={newState => console.log('newState', newState)}
      >
      <App />
    </Provider>
  );
}

Connect any of your app's child components to the global store with the connect higher order component. Any connected component will have state and setState passed as props. Treat them as a global this.state and this.setState. This means you can call setState in one component and have that update reflected in another connected component without prop drilling or callbacks. This also means the global state will persist even when a connected component is unmounted.

class Example extends React.Component {
  render() {
    let { state, setState } = this.props;
    return (
      <>
      	<button onClick={() => setState({ num: Math.random() })} />
      	<h1>{state.num}</h1>
      </>
    );
  }
}

export default connect(Example);

If you want to call multiple setStates that rely on the previous state in the same block of code, you will probably have to use a callback or functional setState because setState, like this.setState, updates state asynchronously.

// this one line is equivalent
setState({ num: state.num + 1 }, newState => setState({ num: newState.num + 1 })); // state.num = 2
// to these two
setState(prevState => ({ num: prevState.num + 1 }));
setState(prevState => ({ num: prevState.num + 1 }));  // state.num = 2

// this won't work
setState({ num: state.num + 1 });
setState({ num: state.num + 1 }); // state.num = 1

// this is an exception
setState({ num: ++state.num });
setState({ num: ++state.num }); // state.num = 2

Example

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider, connect } from 'react-one';

const INITIAL_STATE = { num: 0 };

class App extends React.Component {
  render() {
    return (
      <Provider
        initialState={INITIAL_STATE}
        onSetState={newState => console.log('newState', newState)}
        >
        <div>
          <Buttons />
          <Num />
        </div>
      </Provider>
    );
  }
}

class Buttons extends React.Component {
  render() {
    let { state, setState } = this.props;
    return (
      <div>
        <button onClick={() => setState({ num: ++state.num })}>+</button>
        <button onClick={() => setState({ num: --state.num })}>-</button>
      </div>
    );
  }
}

Buttons = connect(Buttons); // if this were in another file, export default connect(Buttons)

class Num extends React.Component {
  render() {
    let { state } = this.props;
    return <h1>{state.num}</h1>;
  }
}

Num = connect(Num); // if this were in another file, export default connect(Num)

ReactDOM.render(<App />, document.getElementById('root'));

Running the example

git clone https://github.com/MiLeung/react-one.git

cd react-one

npm i

npm start

Then in another window

cd react-one/example

npm i

npm start