#How to use subscribe with createStore.

9 messages · Page 1 of 1 (latest)

reef locust
#

The examples in the docs about subscribing to an observable does not work.

  
 // subscribing to an observable
 const unsubscribe = state.subscribe(() => (
    console.log("change")
  )
);
onCleanup(() => unsubscribe());

No matter what I try. The subscribe method and the on never seem to exist on signals and stores.

keen epoch
#
createEffect(() => 
console.log(state))
#
createEffect(on(() => state, () => {
  console.log('change');
}));
reef locust
#

Thanks, I tried but it only called once.

#
const initialStateForm = {
    basics: {
      name: "",
      email: "",
      phone: ''
    }
  }

  const [form, setForm] = createStore(initialStateForm)

  createEffect(on(() => form, () => {
    console.log(form.basics.name);
  }));
#
<input
              type="text"
              name='basics.email'
              value={form.basics.email}
              onInput={(e) => setForm({ ...form, basics: { ...form.basics, email: e.currentTarget.value } })}
            />
keen epoch
#

if you'll be accessing form inside createEffect, there's no need for on, simply do

createEffect(() => {
  console.log(form.basics.name);
});
glad summit
#

If you want to subscribe to all state changes inside a store, you can use "@lyric scarab-primitibves/deep".