Skip to content

Commit 8e6d3bf

Browse files
committed
keep working on post
1 parent c68e452 commit 8e6d3bf

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/cookbook/gradual-compositionapi-migration.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,19 @@ And it was being used in a modal component in `components/modal.vue`:
6060
We could refactor this by creating a `composables` folder and create `composables/useToggle.js` instead. We suggest using a directory named composables so that you can communicate that this is being used slightly differently from a component, it's reusable logic that you can consume.
6161

6262
```js
63-
import { ref, onMounted } from '@vue/composition-api'
63+
import { reactive, toRefs } from '@vue/composition-api'
6464

6565
export function useToggle() {
66-
const isShowing = ref(false)
66+
const state = reactive({
67+
isShowing: false
68+
})
6769

6870
function toggleShow() {
69-
isShowing.value = !this.isShowing.value
71+
state.isShowing = !state.isShowing
7072
}
7173

7274
return {
73-
isShowing,
75+
...toRefs(state),
7476
toggleShow
7577
}
7678
}
@@ -113,4 +115,19 @@ And we can refactor our component as follows:
113115
</script>
114116
```
115117

118+
<iframe src="https://codesandbox.io/embed/refactor-mixin-to-composition-api-hnloh?fontsize=14&hidenavigation=1&theme=dark"
119+
style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;"
120+
title="Refactor Mixin to Composition API"
121+
allow="accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking"
122+
sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
123+
></iframe>
124+
116125
Note that the template stays the same, but we've extracted the logic to use in our script section differently. If, from here, we wanted to use `isShowing` in the Options API, we could access it with `this.isShowing`, just as we normally do with a data property, and similarly, we can access `this.toggleShow` like we would a method.
126+
127+
You may notice we used `reactive` here instead of `refs`. This is intentional- if you're refactoring a codebase, potentially full of many many values, `reactive` translates faster and more directly as the API is closer to Options, you're still using that same object notation.
128+
129+
## In Place of Vuex
130+
131+
It is possible to use the Composition API in place of Vuex and save yourself a dependency. That said, it's not exactly necessary, either. And there are some tradeoffs.
132+
133+
If you're using Vuex, it's very clear exactly what centralized state is being used across the application. Composition API is very flexible, but you may lose that implicit declaration in communication to other fellow maintainers. Our suggestion that if you do use it as a centralized state management store, that you place it in a `store` folder, or something similarly named, so that responsabilities are clear.

0 commit comments

Comments
 (0)