4.0.2 • Published 3 years ago

react-use-mapped-state v4.0.2

Weekly downloads
16
License
MIT
Repository
github
Last release
3 years ago

react-use-mapped-state

NPM JavaScript Style Guide

Install

npm i react-use-mapped-state

Usage -- Basic

import React from 'react';

import { useMappedState } from 'react-use-mapped-state';

const Example = () => {
  const [{ title }, setState] = useMappedState([
    ['title', 'Our first ok title with object'],
  ]);
  const onChangeTitle = () => {
    setState('title', 'Our fantastic new title....with object');
  };
  return (
    <>
      <div>{title}</div>
      <button onClick={onChangeTitle}>Change Title</button>
    </>
  );
};

export default Example;

Usage -- Basic with previousValues using function

import React from 'react';

import { useMappedState } from 'react-use-mapped-state';

const Example = () => {
  const [{ counter }, setState] = useMappedState([['counter', 1]]);
  const onIncremenet = () => {
    setState('counter', prevVal => prevVal + 1);
  };
  return (
    <>
      <div>{counter}</div>
      <button onClick={onIncremenet}>Click to Increment</button>
    </>
  );
};

export default Example;

Usage -- Basic - with Batching

import React from 'react';

import { useMappedState } from 'react-use-mapped-state';

const Example = () => {
  const [{ title }, setState] = useMappedState([
    ['title', 'Our first ok title with object'],
    ['hasTitleChanged', false],
  ]);
  const onChangeTitle = () => {
    setState(
      ['title', hasTitleChanged],
      ['Our fantastic new title....with object', true]
    );
  };
  return (
    <>
      <div>{title}</div>
      <div>Has title Ever been changed? {hasTitleChanged}</div>
      <button onClick={onChangeTitle}>Change Title</button>
    </>
  );
};

export default Example;

Usage -- Basic with previousValues using function with batching

import React from 'react';

import { useMappedState } from 'react-use-mapped-state';

const Example = () => {
  const [{ counter }, setState] = useMappedState([
    ['counter', 1],
    ['hasCounterBeenClicked', false],
  ]);
  const onIncremenet = () => {
    setState(
      ['counter', 'hasCounterBeenClicked'],
      [prevVal => prevVal + 1, true]
    );
  };
  return (
    <>
      <div>{counter}</div>
      <div>Has Counter Been Clicked? {hasCounterBeenClicked}</div>
      <button onClick={onIncremenet}>Click to Increment</button>
    </>
  );
};

export default Example;

License

MIT © 901david


This hook is created using create-react-hook.

4.0.2

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

3.0.4

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.0.7

4 years ago

2.0.8

4 years ago

2.0.5

4 years ago

2.0.6

4 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago