Ref function holds any value type, including primitives and complex types.
Accessing and modifying ref values requires using the .value property.
Destructuring a ref disconnects reactivity, leading to plain values.
Vue automatically unwraps refs in templates, simplifying usage.
Community preference leans towards using ref by default for simplicity.
In the world of Vue 3, managing reactivity is a vital skill for developing efficient applications. Vue's Composition API provides two main methods for handling reactive state: Ref and Reactive. Understanding the nuances of these tools can significantly enhance how you manage state in your components.
The ref function in Vue 3 is designed to address the limitations found in the reactive function. Unlike reactive, which works exclusively with object types, ref can hold any value type. This includes primitive types such as strings, numbers, and booleans, as well as more complex data types like objects, arrays, or even DOM elements. This flexibility makes ref a versatile tool in the developer's toolkit.
To use ref, you first import it from the Vue package and then create a reactive variable using the ref function. You can pass a primitive value, such as a number zero, or an object, like count zero, to the function. However, when interacting with these reactive variables, it's essential to use the .value property to read or modify them. For example, if you have a ref count initialized to zero, logging count directly returns an object with value zero. To access or change this value, you must use count.value.
Understanding the internal workings of ref can enhance your grasp of its capabilities. The ref function accepts a value, potentially a primitive like a string or number, and creates an object with getters and setters. These use the same track and trigger functions found in Vue's reactivity system. This mechanism allows ref to maintain reactivity, even with primitive values.
It's important to note that when you destructure a ref, the reactivity is lost. If you destructure the value from a count ref, for instance, the resulting variable is a plain number, severing the reactivity connection. Similarly, reassigning count.value to a new variable also results in a loss of reactivity. Vue provides the unwrap function to simplify working with refs. This utility checks if a variable is a ref and returns the unwrapped value if it is.
Using ref inside the script tab requires calling .value. However, Vue automatically unwraps refs in the template, eliminating the need for .value. This seamless integration of ref in templates is a significant advantage, simplifying the development process.
Comparing ref and reactive reveals strengths and weaknesses in both approaches. Ref is adaptable to any value type, while reactive is limited to objects. Ref allows reassignment, making it a flexible choice for dynamic applications. Reactive, however, integrates more naturally with Vue's templates and is beneficial when migrating to the Composition API.
Grouping refs within a reactive object is a recommended pattern. This approach allows you to define separate watchers for each ref while maintaining a coherent structure. Updating a ref, such as setting a loading value to false, triggers all relevant watchers, ensuring consistent state management.
JavaScript, by default, lacks a reactivity system, necessitating Vue's implementation. Vue's reactivity system intercepts reading and writing of object properties, using proxies for reactive objects and getters-setters for refs. This system ensures that changes in the model automatically reflect in the view, a key feature for any modern web framework.
While ref offers versatility and ease of use, reactive excels in scenarios requiring deeply reactive states. Reactive's limitations include its restriction to object types and the different identity between proxy and original objects. When destructuring reactive properties, reactivity disconnects, leading to potential bugs. The twoRefs function, however, allows you to destructure without losing reactivity.
Community feedback shows a preference for ref, with many developers using it by default and reactive when grouping is necessary. This consensus highlights ref's simplicity and effectiveness in managing state. Ultimately, both ref and reactive are powerful tools, and choosing between them depends on the specific needs of your application. Maintaining consistency in your approach will lead to more manageable and reliable code.
There are two approaches to add a reactive state to Vue components using the Composition API. As a result, you must choose whether to utilize reactive(), ref(), or both. I'll guide you in making the best decision.
This talk has been presented at Vue.js London 2023, check out the latest edition of this JavaScript Conference.
The Reactive function in Vue 3 only works on object types (like objects, arrays, or collections such as Map or Set) and the returned proxy object from Reactive does not have the same identity as the original object, which can lead to issues when destructuring or reassigning.
Vue 3 handles reactivity for primitive values through the Ref function, which wraps the primitive in an object with getters and setters to facilitate reactivity.
A recommended pattern is to group Refs inside a Reactive object, which allows for easier management of related reactive properties and ensures that updates to individual Refs trigger reactivity as expected.
Ref is generally preferred because it explicitly shows reactivity with the '.value' syntax, works with any type of value, and maintains reactivity across reassignments and function passes. It's also easier to see at a glance that a value is reactive.
While Ref and Reactive can both be used to create reactive variables, they are not completely interchangeable due to their different handling of reactivity and types of values they support. Ref is versatile with all value types, but Reactive is specifically efficient for reactive object properties.
Ref can hold any type of value and requires accessing values with '.value', suitable for both primitives and objects. Reactive is used primarily for objects and does not use '.value' to access properties but can lose reactivity when destructuring or reassigning objects.
In Vue 3's composition API, you can add reactive state using either 'Ref' or 'Reactive'.
This talk compares Rev and Reactive in Vue 3, exploring reactivity and their limitations. It discusses the use of watchers, identity issues, and migration strategies. The talk also highlights the benefits of using the Ref function for better reactivity and the recommended pattern of grouping Refs. Opinions from the Vue community are shared, with a majority preferring Ref over Reactive.
Welcome to my talk, Rev vs. Reactive. I will explain how you can choose whether to use Rev, Reactive, or both. We will explore the basics of reactivity in Vue 3 and compare Reactive and Rev. Finally, I will discuss the opinions from the Vue community and share a recommended pattern for grouping Rev and Reactive.
2. Reactivity in Vue 3
Reactivity in Vue 3 is crucial for keeping the model and Vue in sync. JavaScript is not reactive by default, so Vue uses proxies and getter-setters to implement reactivity. The reactive function returns a reactive proxy object based on the provided object. It's similar to vue.observable in Vue 2 and creates deeply reactive state.
3. Limitations of Reactive in Vue 3
We define a watcher that correctly outputs the initial state. Limitations of reactive include exclusively working on object types and not having the same identity as the original object. Destructuring a reactive object property into a local variable disconnects reactivity. Vue provides a solution with the two refs function. Another problem arises when reassigning a reactive value, causing reactivity connection to be lost.
4. Watcher, Identity, and Migration
Be careful with the watcher not firing and the different identity between the proxy and the original object. Passing a property into a function can cause problems. Reactive is a good choice for composition API migration. Copy everything from data into reactive to migrate components.
5. Ref Function and Reactivity
Ref addresses the limitations of reactive and can hold any value type, including primitive values and complex data types. To create a reactive variable using ref, we import ref from the view package and pass a primitive or object as the initial value. Reading and modifying the reactive variable requires accessing the value property. Internally, ref creates a new object with a getter and a setter, utilizing the same track and trigger functions as reactive. Ref also uses reactive under the hood for object types. However, destructuring a ref or reassigning count.value disconnects reactivity.
6. Reactivity and Refs in Vue 3
To maintain reactivity when working with refs, you can group them in a plain JavaScript object. Passing refs into functions preserves reactivity and is useful for extracting logic into composables. Refs can be replaced with new object references without losing reactivity. Vue automatically unwraps refs in templates and watcher dependencies. The unwrap utility function correctly unwraps refs, making it easier to work with reactive variables.
7. Utilizing Refs and Reactives in Vue.js
unwrap is a sugar function that checks if a variable is a ref and calls .value if it is. Ref can be used with any value, but values are accessed differently in script and template. Ref objects can be reassigned and passed across functions without losing reactivity. My preference is ref because it's easier to identify reactive values. Grouping refs inside a reactive object is a recommended pattern for better organization and easier handling.
8. Opinions from the Vue Community
Opinions from the Vue community show that more than 60% prefer using ref, while 8% use reactive and 22% use both. Michael Thiessen wrote an in-depth article about ref versus reactive, summarizing the opinions of famous people from the Vue community. The recommendation is to use ref by default and reactive when grouping variables. Both ref and reactive are powerful tools in Vue 3, so choose the one you prefer and maintain consistency in your code.
State management is not limited to complex applications and transitioning to a store offers significant benefits. Pinia is a centralized state management solution compatible with Vue 2 and Vue 3, providing advanced devtools support and extensibility with plugins. The core API of Pinia is similar to Vuex, but with a less verbose version of stores and powerful plugins. Pinia allows for easy state inspection, error handling, and testing. It is recommended to create one file per store for better organization and Pinia offers a more efficient performance compared to V-rex.
Nux3 has made significant improvements in performance, output optimization, and serverless support. Nuxt Bridge brings the Nitro engine for enhanced performance and easier transition between Nuxt 2 and Nuxt Read. Nuxt 3 supports Webpack 5, Bytes, and Vue 3. NextLab has developed brand new websites using Docus technology. Nuxt.js is recommended for building apps faster and simpler, and Nuxt 2 should be used before migrating to Nuxt 3 for stability. DOCUS is a new project that combines Nuxt with additional features like content modules and an admin panel.
Vue 3 has seen significant adoption and improvements in performance, bundle size, architecture, and TypeScript integration. The ecosystem around Vue 3 is catching up, with new tools and frameworks being developed. The Vue.js.org documentation is undergoing a complete overhaul. PNIA is emerging as the go-to state management solution for Vue 3. The options API and composition API are both viable options in Vue 3, with the choice depending on factors such as complexity and familiarity with TypeScript. Vue 3 continues to support CDN installation and is recommended for new projects.
In this Talk, the speaker demonstrates how to use Rust with WebAssembly in a Vue.js project. They explain that WebAssembly is a binary format that allows for high-performance code and less memory usage in the browser. The speaker shows how to build a Rust example using the WasmPack tool and integrate it into a Vue template. They also demonstrate how to call Rust code from a Vue component and deploy the resulting package to npm for easy sharing and consumption.
The Talk discusses the recent feature updates in Vue 3.3, focusing on script setup and TypeScript support. It covers improvements in defining props using imported types and complex types support. The introduction of generic components and reworked signatures for defined components provides more flexibility and better type support. Other features include automatic inference of runtime props, improved define emits and defined slots, and experimental features like reactive props destructure and define model. The Talk also mentions future plans for Vue, including stabilizing suspense and enhancing computer invalidations.
This Talk discusses handling local state in software development, particularly when dealing with asynchronous behavior and API requests. It explores the challenges of managing global state and the need for actions when handling server data. The Talk also highlights the issue of fetching data not in Vuex and the challenges of keeping data up-to-date in Vuex. It mentions alternative tools like Apollo Client and React Query for handling local state. The Talk concludes with a discussion on GitLab going public and the celebration that followed.
The Vue3 has been released in mid-2020. Besides many improvements and optimizations, the main feature of Vue3 brings is the Composition API – a new way to write and reuse reactive code. Let's learn more about how to use Composition API efficiently.
Besides core Vue3 features we'll explain examples of how to use popular libraries with Vue3.
Table of contents: - Introduction to Vue3 - Composition API - Core libraries - Vue3 ecosystem
Prerequisites: IDE of choice (Inellij or VSC) installed Nodejs + NPM
If finding errors in your frontend project is like searching for a needle in a code haystack, then Sentry error monitoring can be your metal detector. Learn the basics of error monitoring with Sentry. Whether you are running a React, Angular, Vue, or just “vanilla” JavaScript, see how Sentry can help you find the who, what, when and where behind errors in your frontend project. Workshop level: Intermediate
We'll build a Nuxt project together from scratch using Nitro, the new Nuxt rendering engine, and Nuxt Bridge. We'll explore some of the ways that you can use and deploy Nitro, whilst building a application together with some of the real-world constraints you'd face when deploying an app for your enterprise. Along the way, fire your questions at me and I'll do my best to answer them.
Get started with AG Grid React Data Grid with a hands-on tutorial from the core team that will take you through the steps of creating your first grid, including how to configure the grid with simple properties and custom components. AG Grid community edition is completely free to use in commercial applications, so you'll learn a powerful tool that you can immediately add to your projects. You'll also discover how to load data into the grid and different ways to add custom rendering to the grid. By the end of the workshop, you will have created an AG Grid React Data Grid and customized with functional React components.- Getting started and installing AG Grid- Configuring sorting, filtering, pagination- Loading data into the grid- The grid API- Using hooks and functional components with AG Grid- Capabilities of the free community edition of AG Grid- Customizing the grid with React Components
The Graph is an indexing protocol for querying networks like Ethereum, IPFS, and other blockchains. Anyone can build and publish open APIs, called subgraphs, making data easily accessible.
In this workshop you’ll learn how to build a subgraph that indexes NFT blockchain data from the Foundation smart contract. We’ll deploy the API, and learn how to perform queries to retrieve data using various types of data access patterns, implementing filters and sorting.
By the end of the workshop, you should understand how to build and deploy performant APIs to The Graph to index data from any smart contract deployed to Ethereum.
We love easily created and deployed web applications! So, let’s see what a very current tech stack like Nuxt 3, Motion UI and Azure Static Web Apps can do for us. It could very well be a golden trio in modern day web development. Or it could be a fire pit of bugs and errors. Either way it will be a learning adventure for us all. Nuxt 3 has been released just a few months ago, and we cannot wait any longer to explore its new features like its acceptance of Vue 3 and the Nitro Engine. We add a bit of pizzazz to our application with the Sass library Motion UI, because static design is out, and animations are in again.Our driving power of the stack will be Azure. Azure static web apps are new, close to production and a nifty and quick way for developers to deploy their websites. So of course, we must try this out.With some sprinkled Azure Functions on top, we will explore what web development in 2022 can do.
Comments