question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Effect memoization and immutable data structures

See original GitHub issue

Current design of useEffect requires dependencies to be either primitive values or references to the same object, because shallow equality check relies on Object.is which is an identity check for objects.

The above means that there’s no way to perform structural comparison, which is needed for immutable data structures when identity check fails.

To maintain backwards compatibility a comparator function could be provided as the third argument to useEffect:

useEffect(fn, deps, depsComparator);

The goal here is to preserve an ease of use of the API with immutable data structures in order to provide an idiomatic usage of useEffect in ClojureScript and other environments that rely on immutability e.g. Immutable.js

cc @mhuebert @Lokeh @orestis

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:28
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
mhuebertcommented, Mar 20, 2019

When a user passes in a data literal to useState / useEffect deps / etc., e.g. (useEffect fx [{:data “foo”}])

I think typed-in literals are too small to worry about. From #14476 -

In those rare cases it’s acceptable to pass [JSON.stringify(variables)] as a dependency. “Wait!” I hear you saying, “Isn’t JSON.stringify() slow?” On small inputs, like the example above, it is very fast.

Comparing small structures like {:some "map" :that "I typed in"} is always going to be fast.

0reactions
DjebbZcommented, Mar 22, 2019

Which means that if I want to skip based on a custom equality check (by custom I mean not the one provided by React), I can just do it before calling setState since I have access to both the old and new state.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Memoization: a good use case for a mutable data structure?
The Scala philosophy seems to be to use an immutable data structure unless you have a very good reason not to.
Read more >
Immutability in Javascript, React, and Immutable.js - Medium
The ability to freely mutate your original data structure and thereby propagating changes to all the other objects that reference to it can...
Read more >
React.js Conf 2015 - Immutable Data and React - YouTube
Lee Byron, FacebookImmutable data unlocks powerful memoization techniques and prohibits accidental coupling via shared mutable state.
Read more >
Functional Programming With Java: Immutability
The fundamental idea behind immutability is simple: If we want to change a data-structure, we need to create a new copy of it...
Read more >
How do immutable data structures and statelessness reduce ...
I'm not sure what you mean by “statelessness”, but immutable data structures certainly eliminate mutation side effects; that's the guarantee. · Ordinary classes ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found