SolidJSS
SolidJSโ€ข2y agoโ€ข
5 replies
ATA noob

Stop infinite recursion when updating state

I need to update a store using it's previous value inside a createEffect.

I am using
import { useKeyDownEvent } from "@solid-primitives/keyboard";

to handle keyboard inputs.

i have a store
const [UserSettingsStore, setUserSettingsStore] = createStore({
  showChat: true, ...})


Now according to the docs , I am doing this https://primitives.solidjs.community/package/keyboard#usekeydownevent:
import { useKeyDownEvent } from "@solid-primitives/keyboard";

const event = useKeyDownEvent();

createEffect(() => {
  const e = event();
  console.log(e); // => KeyboardEvent | null

  if (e) {
    setUserSettingsStore("showChat", !UserSettingsStore.showChat)
  }
});

This is causing an infinite loop, since the value update of showChat is retriggering the createEffect.

Is there a way to read current value of UserSettingsStore.showChat in a non-reactive way, ie. prevent it from triggering infinite loop
A library of high-quality primitives that extend SolidJS reactivity
Solid Primitives
Was this page helpful?