#loader state cleared when setting local state

8 messages · Page 1 of 1 (latest)

sterile swift
#

Hello, the code below shows a problem I'm running into, where clicking the button clears loaderData state. Being new to qwik, no doubt problem on my end an any help super appreciated.

Thanks!


New project created using npm create qwik@latest where only src/routes/index.tsx has been modified.

// src/routes/index.tsx
import { component$, useStore } from "@builder.io/qwik";
import type { DocumentHead } from "@builder.io/qwik-city";
import { routeLoader$ } from "@builder.io/qwik-city";

export const useLoaderData = routeLoader$(() => {
  return { name: "malmsteen" };
});

export default component$(() => {
  const loaderData = useLoaderData();
  const localStore = useStore({ x: 1 });
  return (
    <div>
      <pre>localStore: {JSON.stringify(localStore, null, 2)}</pre>
      <pre>loaderData: {JSON.stringify(loaderData.value, null, 2)}</pre>
      <button
        onClick$={() => {
          localStore.x = 2;
        }}
      >
        {localStore.x}
      </button>
    </div>
  );
});

export const head: DocumentHead = {
  title: "Welcome to Qwik",
  meta: [
    {
      name: "description",
      content: "Qwik site description",
    },
  ],
};
sterile swift
#

Hi, any chance somebody can please help? I was curious if loader data is really getting blown away or just not rendered, so I added console.log(JSON.stringify(loaderData.value)); inside the click handler. In doing so, not only does loader data print to console, but the original problem goes away as well (i.e., stays rendered to screen as expected after clicking button instead of disappearing from view).

#
      <button
        onClick$={() => {
          // console.log(JSON.stringify(loaderData.value)); <== Uncommenting this line solve the problem, but why?
          localStore.x = 2;
        }}
      >
oak burrow
sterile swift
#

Thanks Glen, happy to file the issue.

#

Super appreciate your getting back because I assumed issue was lack of my understanding qwik state system

oak burrow
#

No problem. This seems like it will be a big priority to fix, so hopefully it will be resolved asap.