` element with the class "App" that wraps two child components: `
` and ``. These child components are passed certain properties using Vue's attribute binding syntax (`:count`, `:increment`, `:decrement`, and `:width`).
The `
```
The `setup()` function is executed before a component is created and when the props of the component are available.
With the Composition API, we can import standalone functions to help us access Vue's core capabilities within our component. Let's rewrite the counter and width example we've seen above while relying on the Composition API syntax.
```html
```
The `` of our component remains the same but in the `
```
With these changes, our app will function the same as it did before but in a more composable setting.
By using composable functions in the Composition API setting, we were able to break the context of our app down into smaller, reusable pieces that separated the logic.
Using composable functions in Vue made it easier to separate the logic of our component into several smaller pieces. Reusing the same stateful logic now becomes easy since we are no longer confined to organizing our code within specific options in the Options API.
With composable functions, we have the flexibility to extract and reuse shared logic across components. This separation of concerns allows us to focus on specific functionality within each composable function making our code **more modular and maintainable**.
By breaking down the logic into smaller, reusable pieces, we can compose our components using these composable functions, bringing together the necessary functionality without duplicating code. This approach promotes **code reusability** and reduces the risk of code duplication and inconsistencies.
Additionally, using the Composition API provides better **readability** and **understandability** of the component's logic. Each composable function encapsulates a specific aspect of the component's behavior, making it easier to reason about and test. It also allows for easier collaboration among team members, as the code becomes more structured and organized.
Lastly, building Vue apps with the Composition API allows for **better type inference**. Since the Composition API helps us handle our component logic with variables and standard JavaScript functions, it becomes a lot easier to build large-scale Vue applications with a static type system like TypeScript!
## Source
- [patterns.dev/vue/composables](https://patterns.dev/vue/composables)
### References
- [Composables | Vue Documentation](https://vuejs.org/guide/reusability/composables.html)
- [Collection of Vue Composition Utilities | VueUse](https://vueuse.org/)