Search code examples
javascript

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;

Solution

  • 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;