Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$attrs is undefined when component has no props #6263

Closed
greegus opened this issue Aug 1, 2017 · 8 comments · Fixed by #6441
Closed

$attrs is undefined when component has no props #6263

greegus opened this issue Aug 1, 2017 · 8 comments · Fixed by #6441
Labels

Comments

@greegus
Copy link

greegus commented Aug 1, 2017

Version

2.4.2

Reproduction link

https://jsfiddle.net/50wL7mdz/50757/

Steps to reproduce

Access $attrs as object within component context, when no props were provided.

What is expected?

$attrs is an object containing unrecognized props. Therefore I would expect an empty object, when no props are specified on component tag.

What is actually happening?

$attrs is undefined

@posva
Copy link
Member

posva commented Aug 1, 2017

Out of curiosity, what problem are you facing by having an undefined $attrs? Are you accessing $attrs.something? (if so, why?)

edit: I'm also asking because having $attrs equal undefined when there're no attrs can actually be useful

@nickmessing
Copy link
Member

@greegus, in case you're doing something like if ($attrs.myProp) you can do if ($attrs?.myProp) by using babel-plugin-transform-optional-chaining.

@LinusBorg
Copy link
Member

LinusBorg commented Aug 2, 2017

I would judge this to be expected behaviour, but it's debatable.

@posva
Copy link
Member

posva commented Aug 7, 2017

ping @greegus

@chrisvfritz
Copy link
Contributor

Just ran into this and it was definitely unexpected for me that $attrs should ever be undefined.

For example, if you were writing a todo app that starts out with no todos, it would be very strange to make the list begin as undefined instead of []. You'd have to add if statements (or optional chaining) all over your app. This case is similar.

For niche cases when a user wants to check if any $attrs exist, it'd still be possible to use Object.keys($attrs).length, so that case is still covered.

The part that really convinces me this is a bug though, is that $attrs is set to an empty object when a prop is passed to a component - as the OP points out. So currently, really checking if any $attrs exist would require both strategies:

$attrs && Object.keys($attrs).length

@LinusBorg
Copy link
Member

@chrisvfritz convinced me - it's a bug.

@LinusBorg LinusBorg added bug and removed discussion labels Aug 8, 2017
@zxc0328
Copy link
Contributor

zxc0328 commented Aug 24, 2017

@LinusBorg Looks like no one is working on this bug currently. May I have a pr for this?

@LinusBorg
Copy link
Member

Sure!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants