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

immer-path

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

immer-path

## Why

  • 0.1.1
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
3
Maintainers
1
Weekly downloads
 
Created
Source

immer-path

Why

Manipulate JavaScript Object with immer is very easy, but it's hard to manipulate a deep-nested Object.

For example, a <Component /> may handle a state object in the tree.

   root
    / \
   /   \
  2     \
 / \     3
4   5   / \
       9   \
           obj  <--- `<Component />` render and update this
           / \
          6   7

The problem is:

  • How to update obj in root?
  • How to get next state of obj?

If <Component /> hold the obj object, we can find it in root tree and update it, but can not get next obj in next root.

So the solution is give every object a $$path, <Component /> hold $$path of obj, and update & get next value with $$path.

Usage

import {
  initPath,
  producePath,
  findByPath,
} from 'immer-path';

const target = {
  hey: {
    yes: 'hello',
  },
};

// init
const targetPath = initPath(target);

// get path, <Component /> hold this
const nodePath = targetPath.hey.$$path;

const nextData = producePath(targetPath, (draft) => {
  // find obj with path
  const node = findByPath(draft, nodePath) as any;

  // update it
  node.yes = 'yes';
});

FAQs

Package last updated on 09 Aug 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

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