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

Remove data object declaration #111

Merged
merged 5 commits into from
Feb 17, 2020
Merged

Remove data object declaration #111

merged 5 commits into from
Feb 17, 2020

Conversation

CyberAP
Copy link
Contributor

@CyberAP CyberAP commented Jan 10, 2020

@Leokuma
Copy link

Leokuma commented Jan 10, 2020

I've always found that weird too, but there's surely an explanation for that design decision.

However, the object syntax is slightly more readable than the function syntax. Maybe you could add that as a drawback? Simplicity is a big thing in Vue, especially in comparison to React.

I would prefer the object syntax behavior to match the function syntax behavior rather than the other way around.

@CyberAP
Copy link
Contributor Author

CyberAP commented Jan 10, 2020

However, the object syntax is slightly more readable than the function syntax. Maybe you could add that as a drawback? Simplicity is a big thing in Vue, especially in comparison to React.

Those are not interchangeable and do different things, that's why it's not in the drawback section. Even though I agree that it's more readable at the same time it's more confusing for beginners to distinguish between them without knowing how components actually instantiate. It would be sad to encounter an example with an object declaration, paste that into your code and get an unexpected result.

@sqal
Copy link

sqal commented Jan 10, 2020

object declaration should no longer be valid and produce an error with an explanation that only function declaration is valid for a component. It should also contain a link to an API and migration example.

hmm.. I am confused, because it always worked that way. There is appropriate warning in dev mode if you try to define data using plain object instead of function.

https://jsfiddle.net/vp4k9ebt/

@CyberAP
Copy link
Contributor Author

CyberAP commented Jan 10, 2020

object declaration should no longer be valid and produce an error with an explanation that only function declaration is valid for a component. It should also contain a link to an API and migration example.

hmm.. I am confused, because it always worked that way. There is appropriate warning in dev mode if you try to define data using plain object instead of function.

https://jsfiddle.net/vp4k9ebt/

Good point. I missed that it works only on a root component instance. Will update RFC accordingly. Thanks.

@yyx990803
Copy link
Member

yyx990803 commented Jan 20, 2020

I like this. The impact of this change is also minimized due to the root mounting API change in v3 (where you always use a root component).

^ @CyberAP I think the above should be mentioned in the RFC.

@yyx990803
Copy link
Member

This RFC is now in final comments stage. An RFC in final comments stage means that:

The core team has reviewed the feedback and reached consensus about the general direction of the RFC and believe that this RFC is a worthwhile addition to the framework.
Final comments stage does not mean the RFC's design details are final - we may still tweak the details as we implement it and discover new technical insights or constraints. It may even be further adjusted based on user feedback after it lands in an alpha/beta release.
If no major objections with solid supporting arguments have been presented after a week, the RFC will be merged and become an active RFC.

@yyx990803 yyx990803 added 3.x This RFC only targets 3.0 and above breaking change This RFC contains breaking changes or deprecations of old API. core final comments This RFC is in final comments period labels Jan 20, 2020
@michaeldrotar
Copy link

I like this.. shared state is still easy to achieve and can appear more intentional now.

const shared = {
  foo: 1,
  bar: 2
};

export default {
  data() {
    return {
      localFoo: 'foo',
      shared: shared
    }
  }
};

@JosephSilber
Copy link

You might want to add that if you don't need the this context available in your data factory (which is almost always the case), using an arrow function will get you almost the same syntax as a regular object, avoiding the extra level of nesting:

export default {
  data: () => ({
    look: 'ma',
    no: 'extra',
    indentation: 'required'
  })
}

@cawa-93
Copy link

cawa-93 commented Jan 23, 2020

If this RFC is accepted into the Vue, it will be necessary to further describe in the documentation how the data function differs from the setup function. I think for beginners two very similar features can cause confusion and misunderstanding "Why can't it be done this way?":

data() {
    const state = reactive({
      count: 0,
      double: computed(() => state.count * 2)
    })

    function increment() {
      state.count++
    }

    return {
      state,
      increment
    }
  }

@jacekkarczmarczyk
Copy link

jacekkarczmarczyk commented Jan 23, 2020

Not that I don't agree that some explanation could be added to the docs, but I think that "If this RFC is accepted into the Vue" is not necessary - now you can also have data() function so it can cause same confusion and misunderstanding

"Why can't it be done this way?"

Actually can't it? I guess it would work too, didn't check though

@donnysim
Copy link

I'm honestly not sure what the actual difference is comparing setup to data - they both kind of do the same thing except setup supports returning render function? I wasn't really following the updates concerning setup so would appreciate if anyone could shed some light on this.

@yyx990803
Copy link
Member

yyx990803 commented Jan 23, 2020

Technical differences:

  • data does not receive any arguments. setup receives a reactive props object with a context exposing intentionally minimal amount of properties.
  • data has access to this while setup does not. (Exposing this in setup doesn't offer much but leads to footguns)
  • data can only return objects while setup can return render functions.
  • setup can be an async function (used in combination with Suspense) while data cannot.

Conceptual differences:

  • data is meant for returning state only. There should be no side effects. data is a part of the Options API and is meant to be used with the rest of the Options API.

  • setup is the entry point for Composition API. It allows you to pretty much express everything a component needs. You can setup watchers, register lifecycle hooks or even initiate and wait for data requests in it. Returning state is only a subset of its functionality.

@yyx990803 yyx990803 merged commit f49274c into vuejs:master Feb 17, 2020
@bodograumann
Copy link
Contributor

@CyberAP Please update link to rendered RFC.

@lshapz
Copy link

lshapz commented Mar 12, 2020

updated link to the rendered RFC

@CyberAP
Copy link
Contributor Author

CyberAP commented Mar 12, 2020

Thanks, updated now.

phanan added a commit to vuejs/docs that referenced this pull request Mar 15, 2020
phanan added a commit to vuejs/docs that referenced this pull request Mar 15, 2020
* feat: migrate Style Guide (closes #24)

* fix tag

* remove "data() must be a function because vuejs/rfcs#111
@yyx990803 yyx990803 added feature removal and removed final comments This RFC is in final comments period labels Jul 1, 2020
@bencodezen bencodezen mentioned this pull request Jul 6, 2020
25 tasks
bekovicdusan added a commit to bekovicdusan/Docs that referenced this pull request Mar 30, 2022
* feat: migrate Style Guide (closes #24)

* fix tag

* remove "data() must be a function because vuejs/rfcs#111
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.x This RFC only targets 3.0 and above breaking change This RFC contains breaking changes or deprecations of old API. core feature removal
Projects
None yet
Development

Successfully merging this pull request may close these issues.