cferdinandi / reef

A lightweight library for creating reactive, state-based components and UI.
https://reefjs.com
MIT License
1.16k stars 77 forks source link

Async setting of store value does not always rerender #123

Closed hovancik closed 3 years ago

hovancik commented 3 years ago

Hi,

I am not even sure if this is a reef bug or something else (because I am using electron and not not sure how to reproduce with codepen) but I hoped you might help me with debugging?

So I have setter similar to this:

    initStore: async (props) => {
      const value = await asyncCall()
      props.value = value
    },

and I call it when I render my app:

app.render()
store.do('initStore')

and simply show the value in UI (via template).

I'm setting up store with simple:

  data: {
    value: ''
  },

Let's say my async function returns number 42.

And I noticed this weird behavior:

I've put a lot of console.logs everywhere but value seems to be set correctly, just not shown in UI. I've fixed it like this:

    initStore: async (props) => {
      const value = await asyncCall()
      props.value = value
      app.render()
    },

All then works as expected, but is it good idea?

cferdinandi commented 3 years ago

In this case, I wonder if making the async function a helper that runs initStore() once you get data back is the better option.

async function getData () {
    let value = await asyncCall();
    store.do('initStore', value);
}

initStore: (props, value) => {
      props.value = value;
},
hovancik commented 3 years ago

Thanks for the idea! That indeed seems to work better