pmndrs / koota

🌎 Performant real-time state management for React and TypeScript
127 stars 5 forks source link

useObserve trait value stays undefined when used with world #13

Open Ctrlmonster opened 1 month ago

Ctrlmonster commented 1 month ago

Hey, didn't have the time yet to make a csb for this, but putting it down here so it doesn't get lost:

I had some issues with using useObserve with the world this morning, where the trait value would stay undefined, even though I knew it to exist. Can you double check that it works in one of your apps? (Otherwise I'll make a csb later on)

krispya commented 3 weeks ago

Can you clarify if this is a type or value issue?

Ctrlmonster commented 3 weeks ago

The actual runtime value was undefined

krispya commented 3 weeks ago

Then no, my test and naive set up in an actual app both show it working. Can you reproduce it? Or tell me how to reproduce the issue?

Ctrlmonster commented 3 weeks ago

Yeah I'll see if I can reproduce it in a csb tomorrow

Ctrlmonster commented 2 weeks ago

This should reproduce it:


function SceneContainer() {
  const world = useWorld();

  useEffect(() => {
    world.add(SomeTrait);
    return () => world.remove(SomeTrait);
  }, [world]);

  return (
    <Child />
  )
}

function Child() {
  const world = useWorld();
  const traitSnap = useObserve(world, SomeTrait);

  console.log(traitSnap); // <-- stays undefined until HMR or something else triggers another tree rerender

  return null;
}
krispya commented 1 week ago

Can you verify if this issue still exists with the latest version? This exact set up works with my tests and I believe v0.1.5 fixed it.