cheatcode / joystick

A full-stack JavaScript framework for building stable, easy-to-maintain apps and websites.
https://cheatcode.co/joystick
Other
209 stars 11 forks source link

Consider an instance.children.get() method #343

Open rglover opened 1 year ago

rglover commented 1 year ago

Not sure if this would work but I think it could. If you're building a nested UI, it can be a chore to pass around functions via props. It works, but it's clunky. Ideally, a parent could "call" a child component it renders and access its instance.

The parts are there to do it (everything is just a tree in memory), but a helper function to "get" a child would be great. For example:

events: {
  'click .parent-button': (event, instance) => {
    const child = instance.children.get('some_id_set_by_child');
    console.log(child.state.cart);
  }
}

Here, we'd get a direct reference to some value state.cart on the child. The some_id_set_by_child could be an additional component option that developers could add optionally. If there are multiple instances, instance.children.get() could return an array of all instances matching that id.

rglover commented 2 months ago

This would work quite well now if the component has an id set. Just look in the tree for that ID and get its instance. Should return as an array in case there are multiple instances (i.e., const children = instance.children.get('shared_id')).