Implement 'load more' feature efficiently
I read the docs and searched this for like 3 hours but still can't find a good solution.
Here is what my code currently looks like:
You don't need to care about the
Post
type and component they are just there to display a post
My idea was to use an array to save the loaded posts and when it needs to load more I changed the id to be the last loaded post id11 Replies
when you do
outside reactive scope (function / jsx / effect), it's assigned only once and doesn't wait for response
I'd try something like that
Didn't exactly check if it works, but it's something you can base on.
The
prev
in memo holds previous value that it returned, or inital value before it returned anything. The memo is only depended on loadedPosts()
so it's called directly, it shouldn't trigger when startId
changes, so it's untracked. Then fill previous array with new values and return copy of array, to trigger other updates. If we returned only posts
instead of [...posts]
, then wherever allPosts()
is used, wouldn't be updated, because it would be same object. We don't need to update it though when there are no new posts loaded yet, thet's why after if (start < 0 || !posts)
we have just return prev;
Or you can try using something ready like https://primitives.solidjs.community/package/pagination
Solid Primitives
A library of high-quality primitives that extend SolidJS reactivity
The point is that I don't waste memory to create a new list though
Btw here's my solution after a while
props.list
is the initial list passed from astro
previously I will have to add onMount(loadMorePosts)
to load initial posts
Can you check if there's any problem hereseems fine
I wouldn't worry about creating new list in order to save memory, it does single copy, and then the old lists are unused, so they are garbage collected at some point. Also this is shallow copy, you don't create the copies of every element in list, they are the same objects that are in old list, only references are copied (or pointers if you are familiar) to the new list - it really doesn't take much space.
btw what is
untrack
in your code?createMemo
is recalculated everytime one of it's dependecies changes. Dependencies are the signals used inside function. It recalculates when loadedPosts()
are changed, but I don't want it to recalculate after startId
changes. So createMemo
ignores signals that are inside untrack
function, it's a shorthand for untrack(() => startId())
It works the same for all effect hooks like
createEffect
and createRenderEffect
right?yes
also simple functions, no need to write
const x = createMemo(() => { ... })
, can be just const x = () => { ... }
, but then the result is not cached, and always updates wherever x
is usedhave you seen tutorial of solidjs? it's interactive and gives quite some depth on how everything works
https://www.solidjs.com/tutorial/introduction_basics
SolidJS
Solid is a purely reactive library. It was designed from the ground up with a reactive core. It's influenced by reactive principles developed by previous libraries.
I learn faster by just looking at the docs lol
I saw it but never actually tried it
I'm using Solid with Astro to build some sites
This is my first time using Solid since most of the time I build backend libraries
this tut was fun for me, so I recommend it