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

create-mutable-context

v0.9.2

Published

wrap new React.createContext() to provide mutate function for Consumer

Downloads

9

Readme

really minimal wrap on new React.createContext() to provide context with a set function for Consumer

NOTE

According to 0002-new-version-of-context, mutation is discouraged. But in some situations, Consumer really need a way to update the Provider. So, I come up with this module.

Usage

import createMutableContext from 'create-mutable-context'

const { Provider, Consumer } = createMutableContext()

<Provider defaultValue={1}>
  <Consumer>
    {ctx => {
      // ctx contain value and a set function
      return (
        <button onClick={() => ctx.set(ctx.value + 1)}>
          {ctx.value}
        </button>
      )
    }}
  </Consumer>
</Provider>

ctx.set()

set(updater[, callback])

set function interface is similar to react component's setState(), which accept updater as object or function.

updater as function with the signature:

(prevValue, providerProps) => newValue

set will just replace the value. It will NOT merge newValue to prevValue.

keep value in your own state by Provider.onChange

you can also keep value in your own state (like Input)

import createMutableContext from 'create-mutable-context'

const { Provider, Consumer } = createMutableContext()

class App extends React.Component {
  state = { valueA: 1 }
  render() {
    return (
      <Provider
        value={this.state.valueA}
        onChange={valueA => this.setState({ valueA })}
      >
      ...
      </Provider>
    )
  }
}

access via ctx.foo instead ctx.value.foo

Can use createStateMutext to mutate whole provider state instead of a value field only.

import { createStateMutext } from 'create-mutable-context'

const C = createStateMutext({ foo: 1 })

const App = () => (
  <C.Provider>
    <C.Consumer>
      {ctx => (
        <button
          onClick={() => ctx.set({ foo: ctx.foo + 1 })}
        >
          {ctx.foo}
        </button>
      )}
    </C.Consumer>
  </C.Provider>
)

createObservableMutext

Consumers can observe part of changes easily by names. Names are auto calculate to bitmask

import { createObservableMutext } from 'create-mutable-context'

const C = createObservableMutext({ foo: 0, bar: 0 }, { foo: {}, bar: {} })

const App = () => (
  <C.Provider>
    <C.Consumer
      // observe to foo and only render if foo is changed
      observe="foo"
    >
      {ctx => `Foo: ${ctx.foo}`}
    </C.Consumer>
    <C.Consumer
      // observe to bar and only render if bar is changed
      observe="bar"
    >
      {ctx => `Bar: ${ctx.bar}`}
    </C.Consumer>
    <C.Consumer
      // observe to bar OR foo and render if bar OR foo are changed
      observe="bar,foo"
    >
      {ctx => `BarOrFoo: ${ctx.bar} ${ctx.foo}`}
    </C.Consumer>
  </C.Provider>
)

use option to add functions to ctx

createMutableContext signature:

import createMutableContext from 'create-mutable-context'

const options = {
  // init at the end of provider constructor
  providerConstruct: (provider) => {},

  // init at the end of consumer constructor
  consumerConstruct: (consumer) => {},

  // prepare ctx to pass to consumer children function
  consumerCtx: (ctx, consumer) => ctx,
}
createMutableContext(defaultValue, calculateChangedBits, option)

const C = createMutableContext(defaultValue, null, {
  providerConstruct(provider) {
    Object.assign(provider.state, {
      inc1() {
        this.set(prevState => { value: prevState.value + 1 })
      }
    })
  },  
})

const App = () => (
  <C.Provider>
    <C.Consumer>
      {ctx => (
        <button
          // can call inc1
          onClick={ctx.inc1}
        >
          {ctx.value}
        </button>
      )}
    </C.Consumer>
  </C.Provider>
)