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

use-immer

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-immer

Use immer with React hooks

  • 0.11.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created

What is use-immer?

The use-immer package is a React hook that allows you to work with immutable state in a more convenient way. It leverages the immer library to provide a simple API for updating state without directly mutating it, making state management more predictable and easier to reason about.

What are use-immer's main functionalities?

Basic State Update

This feature allows you to update the state immutably. The `updateState` function takes a callback with a draft state that you can modify directly. The changes are then applied immutably to the original state.

const [state, updateState] = useImmer({ count: 0 });

const increment = () => {
  updateState(draft => {
    draft.count += 1;
  });
};

Nested State Update

This feature allows you to update nested state properties immutably. You can directly modify nested properties within the draft state, and immer will handle the immutability for you.

const [state, updateState] = useImmer({ user: { name: 'John', address: { city: 'New York' } } });

const updateCity = () => {
  updateState(draft => {
    draft.user.address.city = 'Los Angeles';
  });
};

Array State Update

This feature allows you to update array state immutably. You can use array methods like `push` directly on the draft state, and immer will ensure the original array remains unchanged.

const [state, updateState] = useImmer([1, 2, 3]);

const addItem = () => {
  updateState(draft => {
    draft.push(4);
  });
};

Other packages similar to use-immer

Keywords

FAQs

Package last updated on 11 Dec 2024

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