top | item 38732833

(no title)

lambda_garden | 2 years ago

I've never had the need to clone anything in a real JS code-base, personally.

What's the use-case?

discuss

order

VPenkov|2 years ago

It is very frequently needed when you're working with a component framework like React or Vue. Typically leaf components shouldn't mutate properties directly but rather emit a changed version of the original data they receive.

But it's not necessarily related to frameworks; if you're working with complex enough data structures and you're following a functional approach, you'll need to do similar things sooner than later.

lambda_garden|2 years ago

I use React.

Why does that require deep clones?

I simply do:

    return {
      ...current
      foo: "bar"
    };

Gare|2 years ago

When the caller passes you a deep structure and you want to ensure they don't mutate it afterwards. But I agree, it's seldom needed in application code.

mcv|2 years ago

I came across a bug recently where a data structure from Redux, which is immutable, was passed to a GraphQL library that would modify the data it was passed. So we had to make a deep clone.

nec4b|2 years ago

Every time you get an object from somewhere, which needs to be preserved and modified at the same time.