Skip to content

How to check if a $state has changed #15269

Answered by webJose
hyunbinseo asked this question in Q&A
Discussion options

You must be logged in to vote

There's nothing wrong with that boilerplate. Just think about the definition of "has changed". It means "its value went from one to another". In software terms, it means you must compare the current value with the previous one. What you call boilerplate is the exact definition of what you're after.

You can either encapsulate this in a reusable class, or you can use the Previous class from @svecosystem/runed source

Also, to answer the question, you can exchange one boilerplate for another:

let firstRun = true;

$effect(() => {
  // This runs on initial mount as well
  isOpen;
  if (firstRun) {
    firstRun = false;
    return;
  }
  console.log(Date.now(), isOpen);
});

Too bloated. Not sup…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@hyunbinseo
Comment options

Answer selected by hyunbinseo
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