GitXplorerGitXplorer
t

react-mutable-context

public
6 stars
0 forks
0 issues

Commits

List of commits on branch master.
Unverified
6f6e8c735fcc3dd44b43bc25bf4023d109206961

1.0.1

ttargos committed 5 years ago
Unverified
ede06d396ec71c5e27c1392ddb23b9e9a4d6a589

fix: update state if value changes before subscription

ttargos committed 5 years ago
Verified
c2ec3e8a89504e8e0521c0296b0a998dfb510a7d

fix readme

ttargos committed 6 years ago
Verified
8e3e3b4033d84ab30218dce15273d9c40a093db2

chore: update dependencies

ttargos committed 6 years ago
Unverified
ba5dedb20b6639c586214b7c936a00fab6abf015

1.0.0

ttargos committed 6 years ago
Unverified
24a7619ef73500a07d2fb2feff587de0e9af591e

feat: add getValue function

ttargos committed 6 years ago

README

The README file for this repository.

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