Kirk Ross
Kirk Ross

Reputation: 7153

Why does my linter warn “Use object destructuring (prefer destructure)”?

I'm getting a lint warning for this but I'm not sure how to destructure it, or why something so generic would need to be destructured.

const href = window.location.href;

Upvotes: 0

Views: 769

Answers (2)

Neel Rathod
Neel Rathod

Reputation: 2111

Lint given you warning because the same keyword is using for declaring a variable href and also same keyword is used for accessing an object value window.location.href. That's why lint gives you warning.

If you try with different variable name then warning will gone

const ref = window.location.href;

If you don't want to change a name then you can destructuring like below

const { href } = window.location;

Upvotes: 5

Wyck
Wyck

Reputation: 11750

I'm assuming this is because you choose to assign a variable that shares a name with the property you are accessing. In your case, you assigned it to href and you accessed the property href of window.location.

Any such const foo = obj.foo; pattern would seem to be a candidate for doing destructuring with:

const { foo } = obj;

In your specific case, it is suggesting:

const { href } = window.location;

Upvotes: 0

Related Questions