1.4.0 • Published 3 years ago

react-complex-state v1.4.0

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

react-complex-state

version downloads license

A custom hook that makes it easy when working with complex state in react.

When to use it?

Consider using it when you have a complex state, such as an array of strings or array of objects. Essentially, the state must contain an array of any data type.

Installation

Npm :

$ npm install react-complex-state

Yarn :

$ yarn add react-complex-state

Pnpm :

$ pnpm add react-complex-state

Usage

Pass the state you want to use in useComplexState hook like you do in useState.

import { useComplexState } from "react-complex-state";

function App() {
  const complexState = useComplexState(["John", "William"]);

  return (
    <>
      <div>
        {complexState.value.map((item) => (
          <p key={item}>{item}</p>
        ))}
      </div>
      <button onClick={() => complexState.insert("Noah")}>Add</button>
    </>
  );
}

export default App;

Returns

If you see Partial<T> or just T, think of it as the type of state you passed to useComplexState hook.

returntypedescription
valuearrayThe data of your state.
setValuefunctionThe usual setState function. Use it if you want control over your state.
countfunction(filter?: Partial<T> \| undefined) => number Counts number of data that match filter in your state.
findfunction(filter?: Partial<T> \| undefined) => T[] \| [] Finds all the data that match filter in your state. If your don't pass any filter and use it like complexState.find(), it'll be equivalent to complexState.value.
findOnefunction(filter: Partial<T>) => T \| null Finds one data that match filter in your state. If multiple data are found, it'll only return the first one.
insertfunction(data: T, index?: number) => void Add data to your state at an index. By default it will add the data at the end.
insertManyfunction(data: T[], index?: number) => void Add array of data to your state at an index. Defaults are similar to insert function.
updatefunction(data: T, index: number) => void Update your state data at an index.
partialUpdatefunction(data: Partial<T>, index: number) => void Update only the data that you passed at an index. Other remaining data will be unchanged. (Only usable when your state contains array of objects)
partialUpdateManyfunction(data: Partial<T>, indexes: number[]) => void Similar to partialUpdate function but update multiple data at once.
removefunction(index: number) => void Delete your state data at an index.
removeManyfunction(indexes: number[]) => void Delete multiple data of your state at provided indexes.

License

MIT

1.4.0

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago