Machine configuration and api usage #696
-
I've just started playing around more with Zag. When configuring a machine, is it valid to use the generated api from within the machine handlers or should I be using A practical example; I want the menu machine to set a specific entry as highlighted when opened so I did this (solidjs): const [state, send] = useMachine(
menu.machine({
id: createUniqueId(),
onOpen() {
api().setHighlightedId("2");
},
})
);
const api = createMemo(() => menu.connect(state, send, normalizeProps)); This feels slightly awkward with the use of |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
@martinpengellyphillips you can use const [state, send] = useMachine(
menu.machine({
id: createUniqueId(),
highlightedId: '2'
})
); |
Beta Was this translation helpful? Give feedback.
-
@anubra266 - I tried that as well, but it doesn't seem to have any effect for me. Also, would it react to updates? |
Beta Was this translation helpful? Give feedback.
Using
highlightedId
in the initial context was not working for me because there was a delay setting the initial options ahead of the machine being ready (andhighlightedId
only applies on first pass).In my case, using the
api
inonOpen
seems fine, or I can call the underlying instead of api: