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-context-mutation

v0.2.2-Alpha

Published

to replace redux,Provider value contain useActions which is to merge context state value and useActions return an immutable function collection called `actions`

Downloads

12

Readme

English | 中文

NOTE

react-context-mutation is a lighter and more convenient state manager designed for react applications. It aims to replace the Redux in react applications and solve the problems of only one Store in Redux and Non Pluggable state maintenance in the project.

Install

npm install react-context-mutation

Usage

// ./App/context.js
import createAppContext from 'react-context-mutation';
import state from './state'; // initial state tree
import configReducer from './config-reducer'; // pre reduce config function

export default createAppContext(state, configReducer)
// ./App/index.js
import AppContext from './context'
import Header from './Header'

const { AppProvider, AppConsumer } = AppContext

export default function App() {
  return (
    <AppProvider>
      <AppConsumer>
        {({ context, useActions }) => (
          <Layout>
            <Header context={context} useActions={useActions} />
            <Layout>
              <Sider context={context} useActions={useActions} />
              <Content>
                <Router />
              </Content>
            </Layout>
          </Layout>
        )}
      </AppConsumer>
    </AppProvider>
  )
}
// ./App/state.js
export default { // initial state tree
  app: {},
  header: {},
  sider: {},
}
// ./App/config-reducer.js
export default function configReducer({ app = {}, header = {}, sider = {} }) { // pre reduce config function
  return (state) => ({
    app: mergeAppConfig(app, state.app),
    sider: mergeSiderConfig(sider, state.sider),
    header: mergeHeaderConfig(header, state.header),
  })
}
// ./Header/index.js
import createActions from './actions';

export default function Header(props) {
  const { context, useActions } = props;
  const { menu, currentItem } = context.header;
  const actions = useActions('header', createActions); // `header` is namespace, actions is immutable

  const handleMenuChange = useCallback((currentItem) => {
    actions.changeCurrent(currentItem)
  }, [actions]); // actions is immutable

  return (
    <header>
      <Menu menu={menu} currentItem={currentItem} onMenuChange={handleMenuChange} />
    </header>
  )
}
// ./Header/actions.js
export default (mutation, contextRef) => ({ // `mutation`and`contextRef` from closure
  changeCurrent(currentItem) {
    // you can fetch data hear
    mutation.header(() => ({ currentItem }));
    // await for fetch has done, mutation the header context value then
  }
})

createAppContext export

export default is a factory function, receive state tree and a predicted reducer function, return <AppProvider> and <AppConsumer>.

createAppContext(state[, configReducer])

AppProvider

The provider receives a config attribute and make the custom config merge to the abstraction of business framework.

<AppProvider config={/* initial config inner state */}>...</AppProvider>

AppConsumer

The consumer component can subscribe to the change of context. This component allows you to subscribe to context in functional components. useActions return an immutable collection of actions used to change context value.

<AppConsumer>
  {({ context, useActions, mutation }) => /* regular context to use */}
</AppConsumer>

Context

Context provides a way to share such values among components without explicitly passing props layer by layer through the component tree, in order to share the data that is "global" to a component tree.

<AppConsumer>
  {({ context }) => // `context` contains the whole tree of state
    <Header context={context} />
  }}
</AppConsumer>

function Header(props) {
  const { context } = props
  const { menu } = context.header // get `header` namespace

  return (
    <header>
      <Menu menu={menu} />
    </header>
  )
}

useActions

useActions is used to obtain the changes of update status in functional components. Accept a key of namespace and a closure funtion that provide mutation and contextRef.

const { useActions } = props
const actions = useActions(namespace, createActions)

Mutation

Mutation is used to update the status of components.

const { mutation } = props
mutation[namespace](reducer)