S
SolidJS4mo ago
FF

How to update date object?

I have the following situation: I have an array of date object and I need to update the date by knowing its index.
var [days, setDays] = createSignal(
[new Date(768523467), new Date(9837465836256)],
);
var [days, setDays] = createSignal(
[new Date(768523467), new Date(9837465836256)],
);
So I tried this approach and it doesn't work.
setDays((d) => {
d[0].setTime(Date.now());

return d;
});
setDays((d) => {
d[0].setTime(Date.now());

return d;
});
------------------------------------------------------
return (
<div>
<For each={days}>
{(day) => {
return <div>{`${day}`}</div>;
}}
</For>
</div>
);
return (
<div>
<For each={days}>
{(day) => {
return <div>{`${day}`}</div>;
}}
</For>
</div>
);
So how can I do that via "mutation" approach? (I don't need immutable way) Whould I use createMutable or createStore for that?
4 Replies
Brendonovich
Brendonovich4mo ago
with a signal you need to update both the array and the date immutably, with a store you'd just need to update the date immutably (as dates aren't tracked by stores) if you do want to use a store then use createStore
FF
FFOP4mo ago
I got it, but still ther it no point in create new date object. Just want to update object an rerender that. "ther it no point in create new date object" for me.
Brendonovich
Brendonovich4mo ago
you need to create a new Date as solid doesn't track the value inside of a Date to know if it changed, it only tracks the object reference as a more general rule Solid doesn't track inside classes except for some circumstances, only plain arrays and objects
Alex Lohr
Alex Lohr4mo ago
Use createTrigger from @solid-primitives/trigger to create a single reactive trigger, then use dirty() whenever you update the Date object using its methods and track() in the functions/memos you use to read from its methods.
Want results from more Discord servers?
Add your server