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

Option to pick a preference in how to handle primitives within data object #5

Open
CyberAP opened this issue Jun 24, 2019 · 3 comments

Comments

@CyberAP
Copy link

CyberAP commented Jun 24, 2019

I would like to be able to choose on how to convert data props to reactive values. There are two methods: value and state and value is the default one now. I would like to pick state instead.

Input:

  export default {
    data() {
      return {
        visible: false,
      }
    },
  }

Current output:

import { value } from 'vue'

export default {
  setup() {
    const visible = value(false)

    return {
      visible
    }
  }
}

Output with state:

import { state } from 'vue'

export default {
  setup() {
    const data = state({ visible: false })

    return {
      ...data
    }
  }
}
@vberlier
Copy link

As far as I understand this wouldn't let you use data.visible anywhere in the setup function since in this case data is the reactive object and spreading its properties simply copies primitive values over to the returned object.

@CyberAP
Copy link
Author

CyberAP commented Jun 24, 2019

In the setup context yes, but it should be always accessed via the data object there, not from the spread. But on the render context it would be mutable and reactive, if I'm not missing something.

Here's an example by Evan: vuejs/rfcs#42 (comment)

import { state, computed as c } from 'vue'

const MyComponent = {
  template: `
    <div @click="increment">{{ count }} {{ double }}</div>
  `,
  setup() {
    const data = state({
      count: 0
    })

    const computed = {
      double: c(() => data.count * 2)
    }

    const methods = {
      increment: () => { data.count++ }
    }

    return {
      ...data,
      ...computed,
      ...methods
    }
  }
}

@Akryum
Copy link
Member

Akryum commented Jun 24, 2019

@CyberAP If you have an object in the data, if will use state already. I'm not sure the spread in the return will work.

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

No branches or pull requests

3 participants