npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-oo-state

v0.0.19

Published

This package aims to solve the problems of complex states in React's functional components. While functional programming in React has many perks, managing a complex state can get messy. Combining the many benefits of object-oriented programming with React

Downloads

217

Readme

use-oo-state

This package aims to solve the problems of complex states in React's functional components. While functional programming in React has many perks, managing a complex state can get messy. Combining the many benefits of object-oriented programming with React's functional programming makes splitting up code and responsibility among classes easier. This makes it easier to create and maintain clean code.

Installation

npm i use-oo-state

import { useOOState } from 'use-oo-state'

const [state, exampleManager, props] = useOOState(ExampleStateManager, initialState, props)

Use case

This package is useful in higher order components or contexts, where the state tends to get messy. This package will make it easy to create fully typed classes to manipulate the state object.

Disclaimer

This package is an early version, and might have performance impact.

How to use

This package consists of three main concepts:

  • StateManager
  • SubStateHandler
  • useOOState

The package holds its own internal state, which ensures that the state version always is the latest when accessed. Therefore, you don't have to pass arguments into methods to access the latest state, instead you can access it directly with this.state when you need it. Under the hood it uses useState to update the reactive state.

StateManager

StateManager is a generic class that will be extended per state you want to use OO State for. It takes two generic arguments: state and props.

How to create a new StateManager:

First we need to create an interface for the state and for the props:

interface ExampleState {
  name: string
  nameErrorMessage: string
  email: string
}

interface ExampleProps {
  userId: string
}

Then we create our new StateManager in a TS file:

import { StateManager } from 'use-oo-state'

export class ExampleStateManager extends StateManager<ExampleState, ExampleProps> {
  
}

This class exposes the state on this.state. The state is mutable and should be updated with a call to this.setState().
The setState method accepts a partial version of the state, e.g.: this.setState({ name: 'newName' }). this.state and this.setState is also directly accessible in SubStateHandlers.

If you ever want to reset the whole state to the initial state, the state manager also exposes the this.resetState() method. That method will reset the state to the initialState and call the onCreated hook.

This class exposes the following lifecycle hooks that can be overriden:

  • onCreated: A hook called when the super class is initialised.
  • onBeforeStateUpdated: A hook called before the state updates with the following params: newState and currentState. This method can be used to manipulate the state objects before it updates, and needs to return the manipulated state object.
  • onStateUpdated: A hook called after the state updates with the following params: newState and oldState. This method can be used to call new methods based on a state update.
  • onPropsUpdated: A hook called when the props updates with the following params: newProps and oldProps. This method can be used to call new methods based on a prop update.

Example:

import { StateManager } from 'use-oo-state'

export class ExampleStateManager extends StateManager<ExampleState, ExampleProps> {
  override onCreated = () => {
    console.log('The manager was created')
  }
  
  override onBeforeStateUpdated = (newState: Partial<ExampleState>, oldState: ExampleState) => {
    console.log('The class is about to update with the following state: ', newState)
    return newState
  }

  override onStateUpdated = (newState: Partial<ExampleState>, oldState: ExampleState) => {
    console.log('The state is updated with the following state: ', newState)
  }

  override onPropsUpdated = (newProps: Partial<ExampleProps>, oldProps: Partial<ExampleProps>) => {
    console.log(`The class received new props: ${newProps}`)
  }
}

SubStateHandler

A SubStateHandler is used to handle code for the state manager. A state manager can have multiple substate handlers. It can have internal state that is not reactive, and has access to the reactive state of the StateManager. In this example, we will use two handlers:

import { SubStateHandler } from 'use-oo-state'

export class ExampleNameHandler extends SubStateHandler<ExampleStateManager> {
   
   readonly updateName = (newName: string) => {
      if (!this.state.name) console.log('No name set') // The state of the StateManager is accessible on this.state
      this.setState({ name: newName )} // To update the state of the StateManager use this.setState
   }
   
   readonly checkNameIsMatchingEmail = (email: string, name: string) => {
     return !email.toLowerCase().includes(names)
   }
}
import { SubStateHandler } from 'use-oo-state'

export class ExampleEmailHandler extends SubStateHandler<ExampleStateManager> { 

   readonly updateEmail = (newEmail: string) => {
      this.setState({ email: newEmail }) // To update the state of the StateManager use this.setState
   }
}

We then need to initialise the SubStateHandlers in the StateManager:

export class ExampleStateManager extends StateManager<ExampleState, ExampleProps> {
  readonly nameHandler = new ExampleNameHandler(this)
  readonly emailHandler = new ExampleEmailHandler(this)
}

If we want to listen to the life cycle hooks:

  export class ExampleStateManager extends StateManager<ExampleState, ExampleProps> {
  readonly nameHandler = new ExampleNameHandler(this)
  readonly emailHandler = new ExampleEmailHandler(this)
  
  override onBeforeStateUpdated = (newState: Partial<ExampleState>, oldState: ExampleState) => {
    if (newState.email) {
       const name = newState.email ?? oldState.email
      if (!this.nameHandler.checkNameIsMatchingEmail(newState.email, name)) {
        newState.nameErrorMessage = 'Name is not matching email'
      }
    }
    return newState
  }
  
  override onPropsUpdated = (newProps: Partial<ExampleProps>, oldProps: ExampleProps) => {
    if (newProps.userId !== oldProps.userId) {
        console.log('User have changed, should probably do some resetting here')
    }
  }
}

useOOState

The useOOState is the connection between React and the Object Oriented State. It is used as a normal hook:

import { useOOState } from 'use-oo-state'

const [state, exampleManager, props] = useOOState(ExampleStateManager, initialState, { userId: '1' })

Then it can be used in a component or a context:

In component:

interface ExampleComponentProps {
  userId: string
}

const initialState: ExampleState = {
  name: '',
  email: '',
  nameErrorMessage: ''
}

const ExampleComponent: React.FC<ExampleComponentProps> = ({ userId }) => {
  const [state, exampleManager, props] = useOOState(ExampleStateManager, initialState, { userId })
  
  return (
    <div>
        <div>
            Hello, {state.name}.
        </div>
        <input onChange={(e) => exampleManager.nameHandler.updateName(e.target.value)} />
    </div>
  )
} 

In context:

const initialState: ExampleState = {
  name: '',
  email: '',
  nameErrorMessage: ''
}

interface IExampleContext {
  exampleState: ExampleState
  exampleManager?: ExampleManager
}

export const ExampleContext = createContext<IExampleContext>({ exampleState: initialState });

const ExampleProvider: React.FC = ({ children }) => {
   const [exampleState, exampleManager, props] = useOOState(ExampleStateManager, initialState, { userId: '1' })
 
  return <ExampleContext.Provider value={{ exampleState, exampleManager }}>{children}</ExampleContext.Provider>;
};

export default ExampleProvider;