Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-hook-component-state

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-hook-component-state

This hook allows you to isolate and manage the state within the component, reducing rendering operations and keeping the source code concise.

  • 1.0.1
  • unpublished
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

React Hook Component State

This hook allows you to isolate and manage the state within the component, reducing rendering operations and keeping the source code concise.

Motivation

As the component complexity increases more state values are needed and a rendering operation is required whenever the state values change. Instead of adding a part of the source code as a separate component file to improve performance, you can use this hook to internally isolate the component state.

Installation

The easiest way to install react-hook-component-state is with npm.

npm install react-hook-component-state

Alternately, download the source.

git clone https://github.com/stegano/react-hook-component-state.git

Examples

To use the Backdrop or Dialog component, you need to create and use an open state value. You can quickly and easily create a component with a state by using the use-component-state hook without creating an open state value inside the current component.

with MUI Backdrop Component

const SomeComponent = () => {
  ...
  const [$backdrop, setBackdropOpen] = useComponentState<boolean>(
    (isOpen, setOpen) => {
      /**
       * MUI Backdrop Component
       * @see https://mui.com/components/backdrop/#main-content
       */
      return (
        <Backdrop open={open} onClose={() => setOpen(false)}>
          <CircularProgress color="inherit" />
        </Backdrop>
      )
    }, 
    true
  );
  return (
    ...
    {$backdrop}
  )
}

with MUI Dialog Component

const SomeComponent = () => {
  ...
  const [$dialog, setBackdropOpen] = useComponentState<{
    isOpen: boolean, content: string
  }>(
    ({ isOpen, content }, setState) => {
      /**
       * MUI Dialog Component
       * @see https://mui.com/components/dialogs/#main-content
       */
      const handleClose = () => {
        setState((state) => {
          return {
            ...state,
            isOpen: false
          }
        })
      }
      return (
        <Dialog open={open} onClose={handleClose}>
          <DialogTitle>Dialog Title</DialogTitle>
          <DialogContent>{content}</DialogContent>
        </Dialog>
      )
    }, 
    {
      isOpen: false,
      content: 'Dialog Content'
    }
  );
  ...
  return (
    ...
    {$dialog}
  )
}

Keywords

FAQs

Package last updated on 24 Dec 2021

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc