#Make createMemo depend on unmentioned signal

1 messages · Page 1 of 1 (latest)

distant moat
#

I have a createMemo that depends on a list of objects

  const items = createMemo(() => props.articles.map((article) => article.id));

now, when I reorder that list of objects, the createMemo does not fire again, since I guess the content staid the same.

Now I defined

  const [reorderTag, setReorderTag] = createSignal(0);

that I increment each time I reorder the list.

Short of

  const items = createMemo(() => {
    console.log(`reorderTag: ${reorderTag}`);
    return props.articles.map((article) => article.id)
  });

how do I make sure it refires? Making logic depend on console.log feels dirty.

final orbit
#

Where you set articles in your parent component ensure it's a new object

#

i.e.
setArticles([...reOrderedArticles])

#

You also don't need console.log

#

You can also just discard instead
const _ = reorderTag()

#

But better yet use 'on' for explicit depend

distant moat
final orbit
#

you also could potential change equals: to false on your signal - presuming you are actually setting it and not just mutating the article array without calling setSignal - this will run the effects even if the equality check is true

distant moat
#

yeah, but I don't want to run the risk of this evauluating unnecessarily

final orbit
#

with [...list] it will evaluate every single time you setArticles if [...list] is the argument, with equals: false it will evaluate every single time too, but without the allocation of a new list

idle oxide
#

U can do createMemo(mapArray(articles, () => ...)) if you want to only run a function a single time per element.

#

mapArray is what <For/> is using under the hood

steep lion
#

btw, memo-s are usually lazy. they will not call by themselves. only effects will

distant moat
steep lion
distant moat
#

in the original question of this thread the problem came fromt he fact that I did reorder the elements in an array but not in fact change the array itself.
using [...list] made sure my array is re-created, at the cost of allocating a new array

distant moat
steep lion
#

I must mention that there are two ways for modify array inside store. one is with produce helper. second is with setArticles setter by key

distant moat
#

what Mattasia said above also should be kept in mind:

with [...list] it will evaluate every single time you setArticles if [...list] is the argument, with equals: false it will evaluate every single time too, but without the allocation of a new list
In my case thats not a problem, to create new arrays when users move items around