This package has been deprecated

Author message:

This package is not maintained anymore and there are better ways to do what it does in modern React

react-mutable-context
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

react-mutable-context

NPM version build status Test coverage npm download

Create a React context that can be accessed and mutated with hooks.

Installation

$ npm install --save react-mutable-context

Usage

import { createMutableContext } from 'react-mutable-context';
 
const context = createMutableContext('black');
 
const { Provider: ColorProvider, use: useColor } = context;
 
function App() {
  return (
    <ColorProvider>
      <ColorUser />
    </ColorProvider>
  );
}
 
function ColorUser() {
  const [color, setColor] = useColor();
 
  const handleClick = () => setColor('red');
 
  return (
    <div style={{ color }}>
      <div>Using color from the context!</div>
      <div>
        <button type="button" onClick={handleClick}>
          Change color
        </button>
      </div>
    </div>
  );
}
 
// The value can also be read and changed outside of React components
setTimeout(() => {
  console.log(context.getValue()); // 'black'
  context.setValue('green');
  console.log(context.getValue()); // 'green'
}, 1000);

License

MIT

Package Sidebar

Install

npm i react-mutable-context

Weekly Downloads

3

Version

1.0.1

License

MIT

Unpacked Size

10.7 kB

Total Files

12

Last publish

Collaborators

  • targos