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

A util method to add computed to state #86

Closed
thelinuxlich opened this issue Feb 13, 2021 · 6 comments · Fixed by #94
Closed

A util method to add computed to state #86

thelinuxlich opened this issue Feb 13, 2021 · 6 comments · Fixed by #94

Comments

@thelinuxlich
Copy link
Contributor

Right now it's natural to add a reactive property to a proxified state object, what's missing is a util method for doing the same with computed:

import { addComputed } from 'valtio/utils'
import state from './store/root' 

addComputed(state, { double(snap) { return snap.counter * 2 } })
@dai-shi
Copy link
Member

dai-shi commented Feb 13, 2021

Hey, this is interesting. Can we drop proxyWithComputed if we support chaining addComputed?

@dai-shi
Copy link
Member

dai-shi commented Feb 13, 2021

Maybe not. I don’t have an idea yet to support nested computeds with addComputed.

@thelinuxlich
Copy link
Contributor Author

If it's possible to add multiple computeds in a single call, I'd say yes

@dai-shi
Copy link
Member

dai-shi commented Feb 13, 2021

OK, I might need to change a signature significantly. It's more for #85. Let's see how you like it.

@dai-shi
Copy link
Member

dai-shi commented Feb 13, 2021

Hmm, my challenges are not very successful.

https://github.com/pmndrs/valtio/tree/feat/add-computed
The result doesn't seem to be very handy as it still requires proxyWithComputed.

https://github.com/pmndrs/valtio/tree/feat/add-computed-2
This one can't support computed setters.

In either case, nested computed is tricky with typescript.

Is there a real use case to add computed afterward?

@thelinuxlich
Copy link
Contributor Author

@dai-shi IMO the first branch accomplishes the feature, although not as you'd like (deprecating proxyWithComputed)

Real use cases:

  • Augmenting a state tree with reactive/computed fields through plugins(or mixins)
  • Dynamic reactive/computed field generation(through a CMS, smart form generator, etc)

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

Successfully merging a pull request may close this issue.

2 participants