vuejs / rfcs

RFCs for substantial changes / feature additions to Vue core
4.86k stars 548 forks source link

How about apply hook? #217

Closed yisar closed 3 years ago

yisar commented 3 years ago
const count = reactive(0)
count() // getter 0
count(count()+1) // setter
count() // getter 1

The mental burden of ref and reactive is too heavy, and its design is very bad

The apply hook is the only way to solve this problem

Its implementation is very simple, just need to integrate get and set into apply.

new Proxy({}, {
  apply(target, ctx ,args){
    if(args.length === 0){
      track()
      return target.apply(ctx,args)
    }else{
      updateComponent(args[0])
      trigger()
    }
  }
})

Please consider carefully, if you can, I will propose RFC and corresponding implementation.

Dante-dan commented 3 years ago

ref or reactive was a rather perplexing discovery

hax commented 3 years ago

Using one function for both getter and setter will introduce a pattern which (IMO) not very common in Vue and also have mental burden.

imtaotao commented 3 years ago

This is not Vue style. (using function everywhere is disgusting to me)

LinusBorg commented 3 years ago

Vue 3 is out. Ref is here to stay. And introducing another official way of handling their use case would be a disservice to the community.

While I see this could be nice for those that like it's style, the window for discussing such an alternative for core primitives such as ref() has closed weeks or even months ago.

jods4 commented 3 years ago

@yisar if you really like that style, though, you can trivally do it in your apps:

import { ref } from "vue"

function r(value) {
  const data = ref(value)
  return function () {
    if (arguments.length === 0) 
      return data.value
    else
      data.value = arguments[0]
  }
}

// Now you can do:
const count = r(0)
count() == 0
count(count() + 1)
count() == 1
yisar commented 3 years ago

@jods4