1.1.1 • Published 5 months ago

react-object-state v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

react-object-state

React hooks for creating state in object form and easier to manipulate.

Example

import { useObjectState } from "react-object-state";

const App = () => {
  const state = useObjectState({
    count: 0,
  });

  const reset = () => {
    state.set("count", 0);
  };

  const increment1 = () => {
    state.set("count", state.count + 1);
  };

  const increment2 = () => {
    state.set("count", (count) => count + 1);
  };

  const increment3 = () => {
    state.setState({ count: state.count + 1 });
  };

  const increment4 = () => {
    state.setState(({ count }) => ({ count: count + 1 }));
  };

  return (
    <div>
      <div>{state.count}</div>

      <button onClick={reset}>reset</button>
      <button onClick={increment1}>increment 1</button>
      <button onClick={increment2}>increment 2</button>
      <button onClick={increment3}>increment 3</button>
      <button onClick={increment4}>increment 4</button>
    </div>
  );
};

export default App;
1.1.1

5 months ago

1.1.0

5 months ago

1.0.0

5 months ago