use this to replace redux,you can use useActions to change context value and useActions return a mutable function collection

Overview

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(createActions); // 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 closure funtion that provide mutation and contextRef.

const { useActions } = props
const actions = useAction(createActions)

Mutation

Mutation is used to update the status of components.

const { mutation } = props
mutation[namespace](reducer)
You might also like...

n.code react application context library

ncode_react_lib Install yarn add @ncodedcode/ncode_react_lib index.tsx import React from "react"; // add this NCApplicationContext.createContext(new

Feb 4, 2022

a babel plugin that can transform generator function to state machine, which is a ported version of typescript generator transform

Babel Plugin Lite Regenerator intro This babel plugin is a ported version of TypeScript generator transform. It can transform async and generator func

Jul 8, 2022

This command line helps you create components, pages and even redux implementation for your react project

This command line helps you create components, pages and even redux implementation for your react project

react-help-create This command line helps you create components, pages and even redux implementation for your react project. How to install it? To ins

Dec 10, 2022

The CryptoVerse is a Cryptocurrency web application developed using Reactjs for providing the latest updates, value statistics, market cap, supply and news regarding the Cryptocurrency market.

The CryptoVerse is a Cryptocurrency web application developed using Reactjs for providing the latest updates, value statistics, market cap, supply and news regarding the Cryptocurrency market.

CryptoVerse - A Crptocurrency Web Application Getting Started with Create React App This project was bootstrapped with Create React App. Available Scr

Oct 26, 2022

Learning how to use redux - a state management library

Learning how to use redux - a state management library

Redux Learning how to use redux - a state management library What is Redux? Redux is a state management library for JS apps. It centralizes applicatio

Jul 18, 2022

Next-gen, highly customizable content editor for the browser - based on React and Redux and written in TypeScript. WYSIWYG on steroids.

ReactPage ReactPage is a smart, extensible and modern editor ("WYSIWYG") for the web written in React. If you are fed up with the limitations of conte

Jan 6, 2023

This app simulates a simple bookstore, and it was created using ReactJS and Redux.

Bookstore About The Bookstore is a website similar to the "Awesome Books" website built in the previous Microverse module (see live version). My goal

Oct 3, 2021

USA Covid-19 Tracker is a mobile-first application built with React and Redux to give precise information about the virus behavior in the United States. Great transitions and user feedback made with plain CSS.

USA Covid-19 Tracker is a mobile-first application built with React and Redux to give precise information about the virus behavior in the United States. Great transitions and user feedback made with plain CSS.

React.js USA Covid-19 Tracker This application allows the public to keep track of the stadistics of the Covid-19 Pandemic in the United Stated. You wi

Oct 25, 2022

Shows how React components and Redux to build a friendly user experience with instant visual updates and scaleable code in ecommerce applications

Shows how React components and Redux to build a friendly user experience with instant visual updates and scaleable code in ecommerce applications

This simple shopping cart prototype shows how React components and Redux can be used to build a friendly user experience with instant visual updates and scaleable code in ecommerce applications.

Feb 1, 2022
Owner
null
experimental project for babel-plugin-mutable-react-state

Goalist Mutable React Example This example is an attempt to work with babel-plugin-mutable-react-state on a simpler project to see how complicated wou

Reaper 1 Jun 7, 2022
A Higher Order Component using react-redux to keep form state in a Redux store

redux-form You build great forms, but do you know HOW users use your forms? Find out with Form Nerd! Professional analytics from the creator of Redux

Redux Form 12.6k Jan 3, 2023
Redux-todos - simple react, redux todos

Redux Todos Please star this repo if you like ⭐ It's motivates me a lot! Getting Started This project was bootstrapped with Create React App. Stack Av

Ruslan Shvetsov 2 Jul 29, 2022
Return an optimized avatar image from a domain name input.

IndieWeb Avatar API A runtime service to extract avatar images from: <link rel="apple-touch-icon"> <link rel="apple-touch-icon-precomposed"> <link rel

Eleventy 45 Aug 28, 2022
Full Application was developed on Next Level Week Return (#8) event by @Rocketseat

Layout • Technologies • Getting started • License ?? Layout You can view the project layout through the link below: Feedback Widget - Figma Rememberin

Elias Gabriel 14 Nov 1, 2022
This simple and small react component can check your repository stars and change theme on light/dark

Repository metrics for react This beauty and easy (4KB) react component can help you add metrics to your repositories also you can change component th

Koma Human 2 Jun 25, 2022
A web application to search all the different countries in the world and get details about them which can include languages, currencies, population, domain e.t.c This application is built with CSS, React, Redux-Toolkit and React-Router.

A web application to search all the different countries in the world and get details about them which can include languages, currencies, population, domain e.t.c This application is built with CSS, React, Redux-Toolkit and React-Router. It also includes a theme switcher from light to dark mode.

Franklin Okolie 4 Jun 5, 2022
Single Page Application built using React, Context API and OMDb API.

Movie Search App This project is a React application with functions to search for movies and add movies to favorites using OMDb API. Home Page Favorit

Efecan Pınar 24 Sep 6, 2022
Nextjs CRUD with React Context API and Tailwind CSS

This is a Next.js project bootstrapped with create-next-app. Getting Started First, run the development server: npm run dev # or yarn dev Open http://

Fazt Web 23 Sep 25, 2022