Clarify the usage of toRefs and toRef for optional props #639
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of Problem
This was originally reported in #562.
There is a problem with using
toRefs
to destructure props when a prop is optional:The documentation seems to encourage this pattern but in practice it fails if the prop is missing as no ref will be created. This not only requires extra work to handle an
undefined
ref but it also causes problems if the prop is subsequently added (e.g. viav-bind="obj"
).Proposed Solution
There are various ways this could be addressed through changes to the library but I've assumed that the current behaviour is correct and should be reflected in the documentation.
In
composition-api-introduction.md
I've just marked all the props asrequired
. I didn't want to address the problem directly on this page so I just made this tweak to justify the use oftoRefs
.composition-api-setup.md
is where I've addressed the problem head-on. The current documentation describes usingtoRefs
as 'safe', which it definitely isn't. I've added a brief explanation and an example of usingtoRef
instead.I've also made footnotes in the API entries for
toRefs
andtoRef
to mention this problem.Additional Information
This is a best practices recommendation for working around a shortcoming of the library. With that in mind, I think it would be best to gather a few opinions on whether this is actually the way we want to go.