Skip to content

Latest commit

 

History

History
46 lines (36 loc) · 839 Bytes

README.md

File metadata and controls

46 lines (36 loc) · 839 Bytes

useSetState

Dead simple React Hook that can replace this.setState

  • Supports updater function or simple object as first argument:
setState({ count: 0 });
setState(prev => ({ count: prev.count + 1 }));
  • Supports optional callback as second parameter:
setState(
  { text: ev.target.value },
  () => inputRef.current.focus()
);

Usage

import React from "react";
import useSetState from "useSetState";

const App = () => {
  const [state, setState] = useSetState({
    count: 0,
  });

  return (
    <>
      {state.count}
      <button onClick={() => setState({ count: state.count + 1 })}>
        Increment
      </button>
    </>
  );
};

export default App;

Made with ❤ by danedavid