Saving components locally (vue2)

Looking for help with passing data from Props to Data in vue.js? Check out this Stack Overflow discussion.

If you're running into issues, take a look at this sandbox example: https://codesandbox.io/s/u3mr8.

Concerned about side effects of copying objects when saving props into data? Learn more here.

For problems related to computed properties without setters, explore this post: Computed property with no setter.

Need advice on getting your component to render correctly? Dive into this sandbox demo: https://codesandbox.io/s/39sgo.

If you have any ideas, suggestions or tips, feel free to share! Your input is greatly appreciated.

Answer №1

It seems like the error is occurring because setting the value from which the computed property is generated within the getter is not allowed. This creates a logical loop where the initial value is being modified while trying to retrieve the computed results. Instead, you can simply return the prop value when initially calling the getter (if the local value has not been set yet).

  get() {
    if (!this.itemSectionPropsLocal["itemSectionCategory"]) {
      return Object.assign({}, this.itemSectionProps)[
        "itemSectionCategory"
      ];
    }
    return this.itemSectionPropsLocal["itemSectionCategory"];
  },
  set(value) {
    this.itemSectionPropsLocal = value;
  },

Additionally, in the setter, you should assign the received value instead of the prop. If you want to update the local values when the prop value changes after mounting, you should use a watcher.

watch: {
  itemSectionProps: {
    deep: true,
    handler(val){
      this.getPropsLocal = Object.assign({}, val["itemSectionCategory"])
    }
  }
}

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

Component importing error with Vue-cal

When I tried to install npm i vue-cal and added this line in my JavaScript file: import VueCal from 'vue-cal' I encountered an error message in the console saying: Failed to resolve module specifier "vue-cal" ...

My Ruby on Rails app seems to be malfunctioning in a major way

Instead of sharing code snippets, I’ve encountered difficulties in getting my jQuery code to work correctly. If you're curious, you can view the issues on Stack Overflow: Why doesn’t this jQuery code work? and This jQuery hide function just does n ...

Having difficulty running lint on Vue 3 TypeScript application, but building process is successful

We are encountering an issue at the moment. We can successfully build our app, but we are facing challenges with linting using the vue tools (vue-cli-service ...). The hot-reloading feature works initially, but upon saving a file, we receive an error mess ...

JSON objects not loading properly in Bootstrap table

I am facing an issue where my ajax script successfully sends JSON objects to the browser, but the table fails to load the JSON object. Here is my Ajax script: $.ajax({ type : "POST", url : "getLabels.jsp", data : "mail ...

Testing dynamic components with Vue.js: A unit testing guide

In my component, I have a fieldgroup that consists of a label, input, and error elements. The input element is imported dynamically based on the prop (settings.input.component) received from the parent component. I wanted to test this component to ensure ...

Unable to detect click event in Vue devtools

My component is supposed to detect if a menu item has a submenu and toggle its visibility accordingly. However, when I click on it, nothing happens and no event is registered in the Vue devtools. Despite following the Vue docs closely and using the same sy ...

Tips for sending multiple forms and having PHP interpret it as a single form using $.ajax

I am working on an email function using $.ajax with 3 different forms that are loaded through an ajax request. When a user clicks on a button, the current form will disappear and a new form will appear. The challenge I am facing is how to send data from al ...

Trouble encountered while trying to utilize a Promise to define a global variable

I am attempting to populate a global variable using a function and then call subsequent functions after the data is retrieved. Below is the current code I have: $(function () { var jsonData = {}; var dataRetrievalPromise = function () { r ...

What is the best way to transfer a value from state to a function as a parameter?

I have been struggling to find a way to pass the value from state (personId) as a parameter to the function (fetchPersonInfo). Despite my efforts, nothing seems to be working. That's why I decided to seek your assistance on this matter. class Page ex ...

Having trouble implementing a cookie on the second domain using the first domain. Can't seem to get it to work properly

Currently, I am working on integrating ajax functionality with PHP. My main objective is to generate a cookie on the page where ajax call is made. The scenario involves two distinct domains - domain1.com and domain2.com. Essentially, the ajax code is imple ...

Executing server side code using client data in next.jsIn next.js, executing server side code with data

Is there a way to extract metadata from a URL that is provided by the user in my Next.js application? To achieve this, I am utilizing a tool called metadata-scraper. However, upon submission of the form by the user, my application encounters an error: ...

Encountering an error message while trying to use `npm i`

I am attempting to set up the environment in order to run tests on JavaScript. I am using Windows 10 and Python 2.7. However, when I input the command 'npm -i', I receive an error message: https://i.stack.imgur.com/j2WXE.jpg To try and resolve ...

Tips for adjusting the font size of choices within the Material UI autocomplete component

Hey there, I'm currently working on a project using the Material Table and I'm looking to adjust the font size of the options in the Material UI Autocomplete. Any tips would be greatly appreciated! Thanks https://i.sstatic.net/ZM17w.png import R ...

Dealing with a surprise JSON error in Express.js using Javascript

Dealing with Unexpected JSON in my express js application using try and catch. I attempted to achieve this as follows: try{ let body = JSON.parse(req.body); }catch(e){ res.json({ error:e }) } However, the Unexpected JSON error is not caught in ...

Is there a way to track and detect alterations to an element using JavaScript or jQuery

Can I detect the addition of a specific CSS class to an element without having to create a new event? ...

When invoking a function within another function, it is essential to ensure that both values are returned within the function

When calling a function within another function function1(){ return this.a } function2(){ return this.b } To output in string format, you can call function1 inside function2 function2(){ var resultOfFunction1 = this.function1(); return resultOfFunction1 ...

Pattern to identify a JSON string with Regular Expressions

Currently, I am working on developing a JSON validator from the ground up and have hit a roadblock when it comes to the string component. My original plan was to create a regex pattern that aligns with the sequence specified on JSON.org: https://i.sstatic ...

Error: The module '../lib/cli' could not be located

As someone new to the world of JavaScript development, I'm encountering an error while working through the backbone_blueprints book. Here's the specific error message I've come across: > <a href="/cdn-cgi/l/email-protection" class="__ ...

The next.js code is functioning properly when run in development mode, but encounters issues when attempting

When using the useAddress() function in run dev, it is returning undefined undefined and then the address when console logged. However, in the run build/start, it only returns undefined. What steps should I take to resolve this issue? import { useAddres ...

Load additional slides in bxslider, monitor when it has reached the last one

I am using a horizontal carousel slider with bxslider, and I have disabled the infiniteLoop feature to prevent the slider from looping. My goal is to fetch additional slides via an AJAX request and add them to the slider once it reaches the end of the cu ...