question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Advanced usage of set for ignoring state changes

See original GitHub issue

It would be nice if we could ignore state on a granular level, for any state change.

Perhaps you have a piece of state that changes very often and you only want to store those changes occasionally. One example would be the a rotation property on a 3D object. Perhaps there is an app where the user can rotate 3D objects. These update every frame but you only want to store the beginning and end state of the user’s rotation. It might be convenient to have this information in the store, but without ignoring most of the changes, you’ll have to click “undo” 100 times just to get back to the beginning of the rotation!

This could be solved by adding an extra argument to the step function:

const useStoreWithUndo = create<StoreState>(set => ({
  rotationX: 0,
  ...
  setRotation: (rotationX) => set(state => ({ rotationX })),
  setRotationWithIgnore: (rotationX) => set(state => ({ rotationX }), true), // Set second arg to true to ignore this change
}));

In a more complex scenario, you may want to use current state to determine whether to set this argument:

const useStoreWithUndo = create<StoreState>((set, get) => ({
  ...
  setRotation: (rotationX) => {
    const shouldIgnore = get().isDragging
    return set(state => ({ rotationX }), shouldIgnore)
  } 
}));

I’m not entirely sure if this is the best API but thought I’d share my thoughts on the problem and a possible solution.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
funwithtrianglescommented, Oct 14, 2021

I think an option to enable/disable this feature would be the safest approach. The hard part now is just getting typescript to play nicely with other middlewares 😵‍💫

1reaction
charkourcommented, Oct 14, 2021

I can review your code later this weekend. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ignore a local change - Product Documentation | ServiceNow
Ignoring a local change prevents updates to a record from generating new versions in the Local Changes list.
Read more >
Advanced Usage · squizlabs/PHP_CodeSniffer Wiki - GitHub
Ignoring Files and Folders. Sometimes you want PHP_CodeSniffer to run over a very large number of files, but you want some files and...
Read more >
.gitignore file - ignoring files in Git | Atlassian Git Tutorial
Git ignore patterns are used to exclude certain files in your working directory from your Git history. They can be local, global, or...
Read more >
Advanced topics on caching in Apollo Client
Advanced topics on caching in Apollo Client. This article describes special cases and considerations when using the Apollo Client cache. Bypassing the cache....
Read more >
Advanced Processor Power state settings - Ignored?
I am experiencing the similar problem. Any changes I do to minimum or maximum processor state, do not persist and last only for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found