Socket
Book a DemoInstallSign in
Socket

usesetstate-ts

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

usesetstate-ts

Manage state for a react component in a single object

unpublished
latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

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>
  );
}

Keywords

insert

FAQs

Package last updated on 05 Nov 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts