Skip to content

Atom with formula or set value at the same time. #2876

Closed Answered by dmaskasky
mar9000 asked this question in Q&A
Discussion options

You must be logged in to vote

I should also mention that the vanilla 'jotai' way to do this is via a push based mechanic rather than a pull based one.

function getNextPrice(unitPrice: number, discount: number) {
  return unitPrice * (1 - discount / 100)
}
function getNextDiscount(unitPrice: number, price: number) {
  return ((unitPrice - price) / unitPrice) * 100
}

const unitPriceAtom = atom(100, (get, set, value: number) => {
  set(unitPriceAtom, value)
  set(priceAtom, getNextPrice(value, get(discountAtom)))
  set(discountAtom, getNextDiscount(value, get(priceAtom)))
})
const discountValueAtom = atom(0)
const discountAtom = atom(
  (get) => get(discountValueAtom),
  (get, set, value: number) => {
    set(discountVa…

Replies: 2 comments 10 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
10 replies
@dmaskasky
Comment options

@dmaskasky
Comment options

Answer selected by mar9000
@mar9000
Comment options

@mar9000
Comment options

@dmaskasky
Comment options

@mar9000
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants