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-new-context-hoc

v1.0.0

Published

This is a library offering an idea to resolve the state management problem.

Downloads

2

Readme

react-new-context-hoc

This is a library offering an idea to resolve the state management problem.

1. Introduction

Though such as redux, mobx libraries have been popularly used in many projects to manager app state, there are still some problems existed. And if you don't want to use these libraries in your projects, what about considering this.

What's this? As we all know, [email protected] proposes New Context API.

Context provides a way to pass data through the component tree without having to pass props down manually at every level.

In fact, we can store the common state in the top Provider component. And in the descendant componet, we can use Consumer to get and update common state. Based on this idea, this library is created.

In addition, this lib also combines hoc to reduce duplicated codes.

1.1 Provider decorator

For Provider component, you can use decorator "Provider" to describe your component. The Provider decorator accepts two params: context and store.

  1. context: the result of React.createContext().
  2. store: common state in yourt component.

1.2 Consumer decorator

For Consumer component, you can use decorator "Consumer" to describe your component. The Consumer decorator accepts two params: context and relatedKeys.

  1. context: the result of React.createContext(). Note: it should be equal to the one used in Provider.
  2. relatedKeys: store's used keys in your Consumer component. Note: if you define the related keys, it can help you filter those useless render.

For more details, you can see the example below.

2. Usage

Before coding, you should install react-new-context-hoc, like this:

npm install react-new-context-hoc --save
  1. At first, you should use React.createContext to create a context and define your store. Like this:
// DemoContext.js
import React from 'react';
export const store = { count: 1, theme: 'red' };
export const demoContext = React.createContext();
  1. Then, let's use decorator to create a provider component. Like this:
// Demo.js
import React from 'react';

import { Provider } from 'react-new-context-hoc';
import { ThemeApp } from './ThemeApp';
import { CounterApp } from './CounterApp';
import { store, demoContext } from './DemoContext';

@Provider(demoContext, store)
class Demo extends React.PureComponent {
  render() {
    console.log('render Demo');
    return (
      <div>
        <CounterApp />
        <ThemeApp />
      </div>
    );
  }
}

export { Demo };
  1. As we use two component (CounterApp and ThemeApp) in Demo.js, let's create them in the following step. Like this:
// CounterApp.js
import React from 'react';

import { Consumer } from 'react-new-context-hoc';
import { demoContext } from './DemoContext';

const MAP = { add: { delta: 1 }, minus: { delta: -1 } };

export class CounterApp extends React.PureComponent {
  render() {
    console.log('render CounterApp');
    return (
      <div>
        <h3>This is Counter application.</h3>
        <Counter />
      </div>
    );
  }
}

@Consumer(demoContext, ['count'])
class Counter extends React.PureComponent {

  onClickBtn = (type) => {
    const { count, updateContext } = this.props.context;
    updateContext({ count: count + MAP[type].delta });
  };

  render() {
    console.log('render Counter');
    return (
      <div>
        <button onClick={() => this.onClickBtn('minus')}>-</button>
        <span style={{ margin: '0 10px' }}>{this.props.context.count}</span>
        <button onClick={() => this.onClickBtn('add')}>+</button>
      </div>
    );
  }
}
// ThemeApp.js
import React from 'react';

import { Consumer } from 'react-new-context-hoc';
import { demoContext } from './DemoContext';

export class ThemeApp extends React.PureComponent {
  render() {
    console.log('render ThemeApp');
    return (
      <div>
        <h3>This is Theme application.</h3>
        <Theme />
      </div>
    );
  }
}

@Consumer(demoContext, ['theme'])
class Theme extends React.PureComponent {

  onChangeTheme = evt => {
    const newTheme = evt.target.value;
    const { theme, updateContext } = this.props.context;
    if (newTheme !== theme) {
      updateContext({ theme: newTheme });
    }
  };

  render() {
    console.log('render Theme');
    return (
      <div>
        <div style={{ width: '100px', height: '30px', margin: '0 auto', backgroundColor: this.props.context.theme }} />
        <select style={{ marginTop: '20px' }} onChange={this.onChangeTheme}>
          {['red', 'green', 'yellow', 'blue'].map(_ => (
            <option key={_}>{_}</option>
          ))}
        </select>
      </div>
    );
  }
}
  1. All is done, now you can use the component Demo in your app. Let's show the result: