@patomation/useglobal

A react hook to use global local state

Usage no npm install needed!

<script type="module">
  import patomationUseglobal from 'https://cdn.skypack.dev/@patomation/useglobal';
</script>

README

React NPM Package Starter

A react hook to use global local state

Installation

yarn add my-component

or

npm install my-component --save

Usage

import { useGlobal } from '@patomation/useglobal'

const Component = (): React.ReactElement => {
  const [state, setState] = useGlobal({ color: 'red' })

  const { color } = state

  return (
    <div style={{
      color: color as string
    }}>

      {`My Color is ${color}`}

      <button onClick={(): void => {
        setState({ color: 'blue' })
      }}>
        {'Change Color'}
      </button>

    </div>
  )
}

Know issues

Jest testing

If you are testing with jest you will need to mock localStorage. I used jest-localstorage-mock and include it in my jest setup files config. See jest.config.js.

jest.config.js Example

module.exports = {
  ...
  setupFiles: [
    'jest-localstorage-mock'
  ]
}