haunted icon indicating copy to clipboard operation
haunted copied to clipboard

Some race-condition when used with lit-element

Open davidsteinberger opened this issue 5 years ago • 3 comments

I used useContext, useState, useEffect in a setup like this:

render() {
  const { docs } = useContext(DBContext);
  const [users, setUsers] = useState([]);
  useEffect(
    () => {
      const dbUsers = docs
      .filter(({ doc: { type } }) => type === 'user')
      .map(user => ({ ...user, isEditing: false }));
      setUsers(dbUsers);
    },
    [docs]
  );

  return html`
    ${users.map(user => html`
      ...
    `)}
  `;
}

However ${users.map()} is not invoked after setUsers() is called.

When I change my update function like below it seems to work:

async update(changedProperties) {
  this.hauntedState.run(() => super.update(changedProperties));
  await this.updateComplete;
  this.hauntedState.runEffects();
}

davidsteinberger avatar Nov 21 '19 23:11 davidsteinberger

Are you using a Haunted / lit-element integration? If so it seems like you want to submit the bug there.

matthewp avatar Nov 24 '19 17:11 matthewp

I just used it in a class that's extended from LitHauntedElement. That, of course, may not be directly related to haunted, but I thought I should mention it. Is it safe to call runEffects() directly after the super.update() as mentioned in that gist?

davidsteinberger avatar Nov 24 '19 20:11 davidsteinberger

I think this is related to the lit-element integration. I think what is happening is that lit-element likely ignores requestUpdate() when it happens inside of an update(). By waiting for updateComplete it starts accepting requestUpdate() again.

matthewp avatar Nov 24 '19 21:11 matthewp