SolidJSS
SolidJSโ€ข3y ago
mizark

Signal set from localStorage not updating

I am setting a signal like:

const initialCart = isServer
    ? data.userData().cart.products
    : localStorage.localCart
    ? JSON.parse(localStorage.localCart)
    : [];
  const [cart, setCart] = createSignal(initialCart);

I then pass the signal into context and use it another file in this function:

const itemsCount = () => {
    const clen = cart().length;
    const c = cart();
    const clen2 = cart;
    let sum = 0;
    if (c) {
      c.forEach((p, i) => {
        sum += p.count;
      });
    }
    return sum;
  };

I then return it as is standard:

    <span>
        {itemsCount()}
      </span>


For some reason it does not update if the ternary is set to localStorage although it will update if I change itemsCount() to a signal that I set to itemsCount() in onMount or
createEffect
if I use createRenderEffect to set the signal, cart()will have the desired value and itemsCount() the desired return but it just does not update the DOM.

I know the server is setting the initial page load and I expect it to load as 0 but I would would expect there to be a way for me on the client to update the DOM before mount and avoid the flash to the updated value.

Any ideas if I can solve this or if solvable with source of data being localStorage?
Was this page helpful?