question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use Array/Object in props as default value

See original GitHub issue

Whether I can use Array/Object as default value in props setting?

{
    props: {
        arr: {
            type: Array,
            default: []
        }
    }
}

If i may, that will be a bug. Because every instance will share the same thing when I not passing target property to this component. Try this fiddle.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:42
  • Comments:18 (2 by maintainers)

github_iconTop GitHub Comments

995reactions
yyx990803commented, Jul 10, 2015

You can return the default value for a prop inside the data function as well:

data: function () {
  return {
    arr: []
  }
}

A warning seems necessary in the case you described. A solution would be:

props: {
  arr: {
    type: Array,
    default: function () { return [] }
  }
}

A little verbose, but better with ES6:

props: {
  arr: {
    type: Array,
    default: () => []
  }
}
209reactions
endoplasmiccommented, Feb 28, 2018

If you ended up here looking for how to do it with an object, it looks like this (take note of the brackets):

props: {
  yourProp: {
    type: Object,
    default: () => ({
      param: value,
      param2: value,
    }),
  },
},
Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue 2 - How to set default type of array in props - Stack Overflow
I have my Vue component, which is taking an array of objects as a prop. I often use prop validation, especially for 'default'...
Read more >
Props | Vue.js
Prop Validation # · All props are optional by default, unless required: true is specified. · An absent optional prop other than Boolean...
Read more >
Default value to object prop not working as I expected : r/vuejs
I have this component where I want to have the properties of a given prop to be optional and have a default value....
Read more >
Typechecking With PropTypes - React
Default Prop Values ; extends React.Component { ; = { name ; 'stranger' } render ; return ( <div> ; </div> ) } ......
Read more >
How to Use Props in Vue.js - freeCodeCamp
“Props” is a special keyword which stands for properties. It can be registered on a component to pass data from a parent component...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found