Cannot Access Vuejs Functional Component Data Again After Loaded

In this postal service, you'll learn virtually functional components and find out how to apply stateless components in your workflow in Vue.

Before y'all start

You lot volition demand the following in your PC:

  • Node.js version ten.x and above installed. You can verify if yous have this version of Node.js by running the command below in your terminal/command prompt:
node -5
  • Visual Studio Code editor (or a similar code editor)
  • Vue'due south latest version installed globally on your machine
  • Vue CLI three.0 installed on your auto

To do this, uninstall the old CLI version first:

npm uninstall -g vue-cli

Next, install the new one:

npm install -g @vue/cli
  • Download a Vue starter projection here
  • Unzip the downloaded project
  • Navigate into the unzipped file and run the control to go on all the dependencies up-to-date:
npm install

Introduction: what are states and instances?

A Vue awarding land is an object that determines the behaviors of a component. Vue awarding states dictate how the component renders or how dynamic it is.

Meanwhile, a Vue instance is a ViewModel that contains options including templates for presentation elements, elements to be mounted, methods, and lifecycle hooks when initialized.

Vue components

Components in Vue.js are usually reactive: in Vue.js, information objects can have a lot of options for concepts you can employ, computed properties, methods, and watchers. Additionally, information objects re-render any time data values change.

By contrast, functional components do not hold country.

Functional components

Essentially, functional components are functions that have components of their own. Functional components practice not have states or instances because they don't keep or rails state. Furthermore, you can't access the construct inside functional components.

Functional components were created for presentation. Functional components in Vue.js are similar to those in React.js. In Vue, developers can apply functional components to build straight-forward, keen components easily past passing context.

Functional components syntax

From the official documentation, a functional component looks like this:

Vue.component('my-component', {   functional: true,   // Props are optional   props: {     // ...   },   // To recoup for the lack of an instance,   // we are now provided a 2nd context argument.   render: office (createElement, context) {     // ...   } })

Creating a functional component

1 key guideline to keep in listen when creating functional components is functional property. The functional property is specified either in the template department of your component or in the script section. The template department syntax looks something like this:

<template functional>   <div> <h1> hi world</h1>   </div> </template>

Yous can specify scripts as a property like this:

export default {   functional: true,   return(createElement) {     return createElement(       "button", 'Click me'     );   } };

Why are functional components important?

Functional components can be executed quickly because they have no state and exercise non go through the same initialization and re-rendering process as components or parts of the template on data value change.

By and large, functional components are useful for presentation or for displaying a loop of items.

Demo

In this introductory demo, y'all'll run across both the single page component type demo with the Vue template and the render part type of functional components.

Single-page functional component

Open your Test.vue file and copy the code cake below into the file:

<template functional>   <div>     <p v-for="brand in props.brands" :central="make">{{brand}} </p>   </div> </template> <script>  export default {   functional: true,   proper name: 'Test',   props: {     brands: Array   } } </script>

The functional indicator in both the script and the template shows that this is a functional component. Note that props can withal exist passed — they are the only data value that can be passed in functional components.

The temporal information props hold can also exist looped through.

Open your app.vue file and copy the code block beneath into it:

<template>   <div id="app">     <img alt="Vue logo" src="./avails/logo.png">     <Test       :brands ="['Tesla', 'Bentley', 'Ferrari', 'Ford']">     </Test>   </div> </template> <script> import Test from './components/Test.vue' export default {   name: 'app',   components: {     Test   } } </script> <way> #app {   font-family: 'Avenir', Helvetica, Arial, sans-serif;   -webkit-font-smoothing: antialiased;   -moz-osx-font-smoothing: grayscale;   text-align: center;   color: #2c3e50;   margin-top: 60px; } </style>

Here, you'll see that the props reference is being used with a colon.

Run the application in the dev server with the following command:

npm run serve

The result in your browser should look like this:

Render functions arroyo

Functional components tin can also comprise render functions.

Developers use render functions to create their own virtual DOM without using the Vue template.

Use a render role to create a new push button nether the cars list. Create a new file inside your projection binder named example.js and copy the code block beneath into the file:

export default {     functional: true,     render(createElement, { children }) {       return createElement("button", children);     }   };

This creates a return role in a functional component to evidence a button and uses the kid node on the chemical element as the push button text.

Open your app.vue file and copy the code block beneath into the file:

<template>   <div id="app">     <img alt="Vue logo" src="./avails/logo.png">     <Test       :brands ="['Tesla', 'Bentley', 'Ferrari', 'Ford']">     </Test>     <Example>      Find More Cars     </Example>   </div> </template> <script> import Exam from './components/Test.vue' import Instance from './Example' export default {   proper name: 'app',   components: {     Exam, Instance   } } </script> <fashion> #app {   font-family: 'Avenir', Helvetica, Arial, sans-serif;   -webkit-font-smoothing: antialiased;   -moz-osx-font-smoothing: grayscale;   text-align: middle;   color: #2c3e50;   margin-acme: 60px; } </style>

If you lot run the application again, you'll see that Observe More Cars — the child node — is now the text for the button. The example component appears as a functional component upon inspection.

Calculation click events

You lot can add click events on the components and include the method in your root component. Withal, you need the data object statement in your render function to admission information technology.

Copy this in your example.js file:

export default {     functional: true,     render(createElement, { data, children }) {       return createElement("button", data, children);     }   };

Now , add your click event in to the root component and Vue volition recogni ze information technology.

C opy th eastward following into your app.vue file:

<template>   <div id="app">     <img alt="Vue logo" src="./assets/logo.png">     <Test       :brands ="['Tesla', 'Bentley', 'Ferrari', 'Ford']">     </Test>     <Example @click="callingFunction">      Notice More than Cars     </Example>   </div> </template> <script> import Test from './components/Exam.vue' import Example from './Case' export default {   name: 'app',   components: {     Examination, Case   },   methods: {     callingFunction() {       console.log("clicked");     }   } } </script>

In addition to the in a higher place instance, there are other arguments you can apply in your functional components listed in the official documentation.

Conclusion

This beginner's guide to functional components can help you achieve quick presentation, display a loop of items, or display simple parts of your workflow that exercise not crave a state.

Experience your Vue apps exactly how a user does

Debugging Vue.js applications can be difficult, especially when there are dozens, if non hundreds of mutations during a user session. If you're interested in monitoring and tracking Vue mutations for all of your users in production, try LogRocket. LogRocket Dashboard Free Trial Bannerhttps://logrocket.com/signup/

LogRocket is like a DVR for web and mobile apps, recording literally everything that happens in your Vue apps including network requests, JavaScript errors, operation problems, and much more. Instead of guessing why issues happen, yous tin can amass and report on what state your application was in when an issue occurred.

The LogRocket Vuex plugin logs Vuex mutations to the LogRocket panel, giving you context effectually what led to an error, and what state the awarding was in when an issue occurred.

Modernize how you debug your Vue apps - Start monitoring for gratuitous.

ingramprignoced1982.blogspot.com

Source: https://blog.logrocket.com/how-to-use-stateless-components-in-vue-js/

0 Response to "Cannot Access Vuejs Functional Component Data Again After Loaded"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel