Xstate: [Feature] Add vue composition API functions

Created on 19 Nov 2019  路  9Comments  路  Source: davidkpiano/xstate

Bug or feature request?

Feature Request

Description:

I think with the upcoming release of Vue 3.0 and its Composition API which is relatively similar in concept to React Hooks, it would be a great addition to port the current xstate-react Hooks to the Vue Composition API. At the moment the Composition API is backported (with a few caveats), so I could start working on a PR if it's wanted

enhancement

Most helpful comment

Yeah I was too late for my PR. @studnitz great work. You are fast.

All 9 comments

@studnitz I would love this. What would a Vue hook look like?

Looking at Vue鈥檚 API - it would look exactly the same as existing useMachine 馃殌. Only the implementation would differ.

@studnitz is Vue鈥檚 setup called once per component鈥檚 instance?

@davidkpiano As @Andarist said. :)
@Andarist The documentation says:

setup is called right after the initial props resolution when a component instance is created. Lifecycle-wise, it is called before the beforeCreate hook.

Thanks for your answer! The last question - is it possible to track current props inside setup? Can't see (looking quickly through it though) any example of this. Are initial props received anyhow there? Can't see any example of receiving them as argument of the setup, nor available at this.

Yes, that's possible.

Arguments

The function receives the resolved props as its first argument:

export default {
 props: {
   name: String
 },
 setup(props) {
   console.log(props.name)
 }
}

Note this props object is reactive - i.e. it is updated when new props are passed in, and can be observed and reacted upon using the watch function:

I made a first Vue Composition API function today. Was inspired by the react useMachine
https://github.com/AleksejDix/vue-use/blob/master/src/compositions/useMachine.js

Yeah I was too late for my PR. @studnitz great work. You are fast.

FYI the alpha for vue@next and @vue/reactivity were released on NPM a couple of days ago.

This was released as @xstate/vue! Currently version 0.1.x

Was this page helpful?
0 / 5 - 0 ratings

Related issues

laurentpierson picture laurentpierson  路  3Comments

carlbarrdahl picture carlbarrdahl  路  3Comments

bradwoods picture bradwoods  路  3Comments

doup picture doup  路  3Comments

rodinhart picture rodinhart  路  3Comments