usesetstate-ts
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

About useSetState-ts

Custom react hook which allows you to use a single object as the state instead of having to use multiple useState hooks.

  • This was written in Typescript and is fully typesafe :)

Installation

  • npm i -s usesetstate-ts

Sample code:

import { useContext } from 'react';
import useSetState, { TSetState } from 'usesetstate-ts';


const LoginPageCxt = React.createContext<ILoginPageCxt>({
  state: { email: '', password: '' },
  setState: (_: Partial<IState>) => { return; },
});


interface ILoginPageCxt {
  state: IState;
  setState: TSetState<IState>; // setState type is exported incase you need it for any reason
}

interface IState {
  state: string;
  email: string;
}


function LoginPage(): JSX.Element {
  const [ state, setState ] = useSetState({
    email: '',
    password: '',
  });
  // Return
  return (
    <div>
      <LoginPageCxt.Provider value={{
        state,
        setState,
      }}>
        <input
          value={state.email}
          onChange={e => setState({ email: e.currentTarge.value })}
        />
        <input
          value={state.password}
          onChange={e => setState({ password: e.currentTarge.value })}
        />

        <SomeOtherChildComponent/>
      <LoginPageCxt.Provider>
    </div>
  );
}

function SomeOtherChildComponent() {
  const { state, setState } = useContext(ILoginPageCxt);
  return (
    <div>
      <button onClick={e => setState({ ... })} />
    </div>
  );
}

Package Sidebar

Install

npm i usesetstate-ts

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

4.51 kB

Total Files

7

Last publish

Collaborators

  • seanpmaxwell1