npm

Need private packages and team management tools?Check out npm Orgs. »

simple-statefull-provider

1.2.0 • Public • Published

simple-stateful-provider

npm package

A simple react hook based provider that can be used for storing simple state for you application.

Following from the amazing pattern by Kent C. Dodds, this package allows you to quickly create new providers with state and set state functions.

Example

First install the package in your project:

npm install simple-statefull-provider

Import the create createProvider function where you want to use the provider:

import createProvider from 'simple-statefull-provider';

Create a new provider using array destructuring, choosing the names for the provider, state hook and set state hook.

const [
    DemoProvider,
    useDemoState,
    useDemoSetState
] = createProvider();    

createProvider also takes an optional initial state value.

Then use the Provider as you would any React provider

//app.js

return (
    <DemoProvider>
        <App/>
    </DemoProvider>
)

View provider context

To read the state from the provider you can use the useDemoState hook.

const ExampleView = () => {
  const state = useDemoState();

  return (
      <p>
        STATE: {JSON.stringify(state)}
      </p>
  )
}

Set provider context

To set the state from the provider you can use the useDemoSetState hook.

const ExampleSet = () => {
  const state = useDemoState();
  const setState = useDemoSetState();

  return (
      <p>
        <input
            value={state || ''}
            onChange={evt => {
              setState(evt.target.value)
            }}
        />
      </p>
  )
}

install

npm i simple-statefull-provider

Downloadsweekly downloads

2

version

1.2.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability