How to deal with on/off listeners on service with TypeScript #1531
-
I hope this is something that's planned to change in V5. The React.useEffect(() => {
// extracted the type, but it's horrible to deal with that
const onTransition: Parameters<typeof service['onTransition']>[0] = (state) => {
}
service.onTransition(onTransition)
return () => {
service.off(onTransition)
}
}) So ideally it should look like this. Small change, but a huge difference in user code. React.useEffect(() => {
return service.onTransition(state => {
})
}) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
Keep in mind that React is only an integration point with XState and its APIs should not influence the API decisions for XState itself. That being said I probably would prefer the proposed change as well. However, this makes patterns like this more awkward to write: interpret(machine)
.onTransition(() => {})
.start() OTOH I feel like |
Beta Was this translation helpful? Give feedback.
-
Answer: #1531 (reply in thread) |
Beta Was this translation helpful? Give feedback.
Answer: #1531 (reply in thread)