Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 useExhaustiveDependencies error on JSON.stringify as dep. #577

Closed
1 task done
Rados51 opened this issue Oct 22, 2023 · 6 comments
Closed
1 task done

🐛 useExhaustiveDependencies error on JSON.stringify as dep. #577

Rados51 opened this issue Oct 22, 2023 · 6 comments

Comments

@Rados51
Copy link

Rados51 commented Oct 22, 2023

Environment information

CLI:
  Version:                      1.3.1
  Color support:                true

Platform:
  CPU Architecture:             aarch64
  OS:                           macos

Environment:
  BIOME_LOG_DIR:                unset
  NO_COLOR:                     unset
  TERM:                         "xterm-256color"
  JS_RUNTIME_VERSION:           "v21.0.0"
  JS_RUNTIME_NAME:              "node"
  NODE_PACKAGE_MANAGER:         "pnpm/8.9.2"

Biome Configuration:
  Status:                       Loaded successfully
  Formatter disabled:           false
  Linter disabled:              false
  Organize imports disabled:    true
  VCS disabled:                 true

Workspace:
  Open Documents:               0

What happened?

  1. Insert JSON.stringify as useMemo dependency.
  2. It throws This hook do not specify all of its dependencies. and This hook specifies more dependencies than necessary. error.

https://biomejs.dev/playground/?code=aQBtAHAAbwByAHQAIAB7ACAAdQBzAGUAUwB0AGEAdABlACwAIAB1AHMAZQBFAGYAZgBlAGMAdAAgAH0AIABmAHIAbwBtACAAJwByAGUAYQBjAHQAJwA7AAoACgBlAHgAcABvAHIAdAAgAGMAbwBuAHMAdAAgAEMAbwBtAHAAbwBuAGUAbgB0ACAAPQAgACgAKQAgAD0APgAgAHsACgAgACAAYwBvAG4AcwB0ACAAWwBvAGIAagBlAGMAdABDAG8AbgB0AGUAbgB0AF0AIAA9AAoAIAAgACAAIAB1AHMAZQBTAHQAYQB0AGUAPAB7AGQAYQB0AGEAOgAgAHMAdAByAGkAbgBnAH0APgAoAHsAZABhAHQAYQA6ACAAIgBuAG8AbgBlACIAfQApADsACgAKACAAIAB1AHMAZQBFAGYAZgBlAGMAdAAoACgAKQAgAD0APgAgAHsACgAgACAAIAAgAGMAbwBuAHMAbwBsAGUALgBsAG8AZwAoAG8AYgBqAGUAYwB0AEMAbwBuAHQAZQBuAHQAKQA7AAoAIAAgAH0ALAAgAFsASgBTAE8ATgAuAHMAdAByAGkAbgBnAGkAZgB5ACgAbwBiAGoAZQBjAHQAQwBvAG4AdABlAG4AdAApAF0AKQA7AAoACgAgACAAcgBlAHQAdQByAG4AIABvAGIAagBlAGMAdABDAG8AbgB0AGUAbgB0ADsACgB9ADsACgA%3D

Expected result

It should show no error.

Code of Conduct

  • I agree to follow Biome's Code of Conduct
@nissy-dev
Copy link
Contributor

nissy-dev commented Oct 22, 2023

Why do you think it should show no error? In my side, eslint-plugin-react-hooks show error, so it should show error.

SS_ 2023-10-22 17 33 06

@Rados51
Copy link
Author

Rados51 commented Oct 22, 2023

@nissy-dev

The first statement (from both Biome and ESLint plugin) is wrong. Dependency is not missing, it is specified in just "a different way".

The only correct way of using objects as dependency in useMemo/useEffect is to convert it to either string or hash it in other ways as comparing objects will never result in true. Using an object will result in re-render every single time, and it would be the same as using useMemo/useEffect without dependencies.

In the case of eslint-plugin-react-hooks both statements are wrong. I guess that second statement could be resolved like const staticDep = JSON.stringify(originalObject);, but that would create an unnecessary variable in memory.

@ematipico
Copy link
Member

ematipico commented Oct 22, 2023

@Rados51

If you think it's a valid bug, you should file an issue to their repository, after all it's maintained by the react core team.

The intention of Biome 's rule is to replicate as much as possible from the original rule, and not deviate from it. Happy to resume the discussion once this is solved.

@ematipico ematipico closed this as not planned Won't fix, can't repro, duplicate, stale Oct 22, 2023
@GabenGar
Copy link
Contributor

Dependency is not missing, it is specified in just "a different way".

There is no "different way", objectContent and the result of JSON.stringify(objectContent) are two different references and react only checks for referential equality.

@Rados51
Copy link
Author

Rados51 commented Oct 23, 2023

Dependency is not missing, it is specified in just "a different way".

There is no "different way", objectContent and the result of JSON.stringify(objectContent) are two different references and react only checks for referential equality.

This is why passing objectContent to hook dependency will always result as false as console.log({a:1}==={a:1}); // false

@GabenGar
Copy link
Contributor

Only if you change the reference with the action provided by the useState() hook or the state is derived exclusively within the component, such as in your example.
The point is the deps array doesn't do advanced introspection for value comparison and neither can the linter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants