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

Use functional approach without mutations? #1770

Open
aryzing opened this issue Nov 25, 2024 · 0 comments
Open

Use functional approach without mutations? #1770

aryzing opened this issue Nov 25, 2024 · 0 comments
Assignees
Labels
feature Brand new functionality. New pages, workflows, endpoints, etc.

Comments

@aryzing
Copy link

aryzing commented Nov 25, 2024

Problem

In reactive FE libraries like React, mutating deeply nested data can make it difficult to sync the UI with the data. For example, setting a new fee mutates the auth property,

setFee(amount: IntegerType) {
this.auth = setFee(this.auth, amount);
}

Even explicitly setting the data may not be sufficient,

function handleFeeChange(newFee) {
  transaction.setFee(newFee); // mutates inner prop
  setTransaction(transaction); // outer transaction object still has same ref. equality
}

Even after the above code, transaction has the same referential equality, which prevents memos from running or components from rendering.

Solution

Having a functional approach which avoids mutations would help with reactive UI frameworks.

The API could look something like,

const nextTransaction = setFee(transaction, newFee);

function setFee(transaction, newFee) {
  return {
    ...transaction,
    auth: createAuth({...transaction.auth, fee: newFee}),
  };
}

Workaround

Currently, the only way to preserve reactivity seems to be with something like

const [auth, setAuth] = useState(transaction.auth); // fee is stored in this object

function handleNewFee(newFee) {
  transaction.setFee(newFee);
  setAuth(transaction.auth);
}

return <div>{auth.fee}</div>

which is suboptimal since transaction is no longer the source of truth for fee, and it's all to easy to accidentally reach out for transaction.auth.fee in a reactive context and break the UI.

@aryzing aryzing added the feature Brand new functionality. New pages, workflows, endpoints, etc. label Nov 25, 2024
@github-project-automation github-project-automation bot moved this to 🆕 New in DevTools Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Brand new functionality. New pages, workflows, endpoints, etc.
Projects
Status: 🆕 New
Development

No branches or pull requests

2 participants