The extent of locally declared variables within a Vue component

Within this code snippet:

<template>
  <div>
    <p 
      v-for="prop in receivedPropsLocal"
      :key="prop.id"
    >
        {{prop}}
    </p>
  </div>
</template>

<script>

export default {
  name: "PropsReceiver",
  props: {
    receivedProps: {        
      required: true,
      type: Array,      
      default() {
        return [];
      },
    },
  },
  data() {
    return {
      receivedPropsLocal: Array,
    };
  },
  methods: {
  },
  watch: {
    receivedProps: {
      deep: true,
      handler(val) {
        let tmp = Object.entries(Object.assign({}, val));
        this.receivedPropsLocal = tmp;
      },
    },
  },
  computed: {
    getReceivedPropsLocal: {
      get() {
        if (!this.receivedPropsLocal) {
          let tmp = Object.entries(Object.assign({}, this.receivedProps));
          this.receivedPropsLocal = tmp;
          return this.receivedPropsLocal;
        }
        return this.receivedPropsLocal;
      },
      set(value) {
        this.receivedPropsLocal = value;
      },
    },
  },
};
</script>

What is the context of tmp? Is it treated similarly to other entries in data(), or does it behave differently?

Answer №1

I think the variable tmp can only be accessed within the handler function because it was declared using let.

To make it accessible throughout the component, consider declaring it directly in the data object.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieving a targeted data point from a JSON object

I am working with a json data that contains various properties, but I am only interested in extracting the uniqueIDs. Is there a way to retrieve ONLY the uniqueID values and have them returned to me as a comma separated list, for example: 11111, 22222? (I ...

When you use the useState object in NextJS, the context object may appear to be empty

I've encountered an issue while trying to pass a context object in NextJS that uses data from a useState hook. Strangely, the state variable and setState functions are undefined when consumed. It's puzzling because substituting a simple variable ...

Troubleshooting issue with JavaScript sorting function failing to arrange elements in ascending

I'm having trouble sorting numbers in ascending order using JavaScript. Here's the code snippet: <h2>JavaScript Array Sort</h2> <p>Click the button to sort the array in ascending order.</p> <button onclick="myFunctio ...

Avoiding unnecessary re-renders of a parent component caused by a child component

I'm facing rendering problems and would greatly appreciate any assistance. I attempted to use useMemo and useCallback, but it resulted in the checkbox breaking. Within a component, I am displaying information from an object. Let's consider the fo ...

React.js: The specified element type is not valid:

I am currently working on a sample project using react.js in Visual Studio 2019 Here is my Index.js file: import 'bootstrap/dist/css/bootstrap.css'; import React from 'react'; import ReactDOM from 'react-dom'; import { Provi ...

Rendering the HTML5 canvas within a console environment

I am looking to create images of humans on the console using nodejs. In order to achieve images of higher quality, I require a library or module that can facilitate drawing images in the console. Some options available include imaging and console-png. How ...

Creating a dropdown menu that interacts with a specific element: step-by-step guide

TL;TR How can I achieve the following: getMyComponent(selectedMyComponentID).complexOperation() This seems like a simple and practical task, such as selecting an item from a dropdown menu. More Information Imagine that I am creating some sort of editor ...

Using Jest: A guide to utilizing a mocked class instance

When working on my frontend React application, I decided to use the auth0-js library for authentication purposes. This library provides the WebAuth class which I utilize in my code by creating an instance like so: import { WebAuth } from 'auth0-js&ap ...

How to implement a self-invoking function in React JS like you would in regular JavaScript?

Is it possible to invoke the function good without triggering it from an event? I want it to run as soon as the page loads, similar to a self-invoking JavaScript function. Check out this example import React from 'react'; class App extends Reac ...

Ensuring Node.js backend JavaScript waits for completion of my bash script before proceeding

Running three bash commands through a Node.js code snippet. Here's a portion of the script: exec(str, function(error, stdout, stderr){ console.log('stdout:'+stdout); console.log('stderr:'+stderr); if(error!=null){ ...

Utilize vue.js to save the components of an object

data() { return: { user: {}, userName: "", userAge: "" } }, methods: { saveUserName: function() { this.userName = this.newUserName; this.$refs.userNameModal.hideModal(); this.$ ...

Protractor Cucumber: Issue with locating spec file patterns (identifying 2 features)

I am facing an issue with running 2 different cucumber features. After adding the following lines to my protractor.conf.js file: specs: ['add.feature', 'delete.feature'] I encountered a problem when running the tests stating pattern ...

What is the most efficient approach to save a value for future utilization in a subsequent function? I've heard that exploiting global variables is highly unfavorable

So I have this code that can be found at http://jsfiddle.net/8j947/10/. It returns either true or false for the variable isLive. My main concern now is how to utilize the variable onLive in a subsequent function. While I've encountered some solutions ...

What is the maximum character limit for the JQuery validation plugin?

Currently, I am utilizing the JQuery validation plugin in my project. My goal is to set a maxlength for one of the fields. Here's an example of how it can be done by defining rules externally: rules: { Message: { required: false, maxLe ...

Present a pop-up notification box with a countdown of 30 seconds prior to the expiration of a session timeout in JSF

Our task is to create a timeout window that appears 30 seconds before the session expires. If the user remains inactive, they will be automatically redirected to the home page. We already have the maximum allowed duration of inactivity defined. I would l ...

How can React and Redux ensure that response data is accessible to every component?

When using react and redux, how can data written in the useDispatch function be made available in other components? Additionally, how can the customerId be accessed in all components? I have created a code that calls an API and returns data in response. I ...

Encountering difficulties with showing contact images in phonegap using angularjs

In my project, I encountered an issue where I can fetch and display the contact photo using simple HTML and JavaScript. However, when I attempt to do the same using AngularJS model, I encounter an error. Below is the code snippet that I am struggling with: ...

Can Comet be implemented without utilizing PrototypeJs?

Can Comet be implemented without utilizing PrototypeJs? ...

Tabindex issue arises due to a conflict between Alertify and Bootstrap 4 modal

When trying to call an Alertify Confirmation dialog within a running Bootstrap 4 Modal, I encountered an issue with the tab focus. It seems to be stuck in the last element and not working as expected. I suspect that this might have something to do with th ...

Setting the offset for panResponder with hooks: A step-by-step guide

While exploring a code example showcasing the use of panResponder for drag and drop actions in react native, I encountered an issue with item positioning. You can experiment with the code on this snack: The problem arises when dropping the item in the des ...