You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi! Great framework! I read the docs on createSignal, createMemo, createComputed multiple times but haven't found a way to solve this problem:
import{render}from"solid-js/web";import{createSignal,createMemo,For}from"solid-js";constITEMS1=[1,2,3,4,5,6,7,8,9];constITEMS2=[11,22,33,44,55,66,77,88,99];functionItemList(){// passed via context, can't know when changesconst[inputItems,setInputItems]=createSignal(ITEMS1);constSHOW_PER_PAGE=3;const[showLastX,setShowLastX]=createSignal(SHOW_PER_PAGE);constallItems=createMemo(()=>{// XXX: reset showLastX when we recalculate input items// XXX: but docs say "The memo function should not change other signals by calling setters"setShowLastX(SHOW_PER_PAGE);returninputItems().map((v)=>v+1);// doing complex calculation with input items});constitems=createMemo(()=>allItems().slice(0,showLastX()));return(<div><ul><Foreach={items()}>{(item)=><li>{item}</li>}</For></ul><buttontype="button"onClick={()=>setShowLastX(SHOW_PER_PAGE)}>
reset page
</button>{" "}<buttontype="button"onClick={()=>{setShowLastX(showLastX()+SHOW_PER_PAGE);}}>
more items
</button>{" "}<buttontype="button"onClick={()=>setInputItems(ITEMS2)}>
change input items
</button></div>);}render(()=><ItemList/>,document.getElementById("app")!);
Basically I want to change signals based on input signal without unnecessary recalculations:
inputItems changed (we don't know why) → reset showLastX, recalculate allItems, recalculate items only once
user scrolled the page → change showLastX, don't recalculate allItems, recalculate items only once
Resetting showLastX at the start of createMemo works but docs say that you shouldn't do that.
I've tried to use createComputed but I'm not sure it's the correct way because docs say it might be executed multiple times but I don't want to recalculate items when allItems and showLastX haven't changed:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi! Great framework! I read the docs on
createSignal
,createMemo
,createComputed
multiple times but haven't found a way to solve this problem:Link to playground
Basically I want to change signals based on input signal without unnecessary recalculations:
inputItems
changed (we don't know why) → resetshowLastX
, recalculateallItems
, recalculateitems
only onceshowLastX
, don't recalculateallItems
, recalculateitems
only onceResetting showLastX at the start of
createMemo
works but docs say that you shouldn't do that.I've tried to use
createComputed
but I'm not sure it's the correct way because docs say it might be executed multiple times but I don't want to recalculateitems
whenallItems
andshowLastX
haven't changed:Any ideas how to do it in idiomatic Solid way? Thanks.
Beta Was this translation helpful? Give feedback.
All reactions