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.

Feature request: add previous global state to addCallback parameters

See original GitHub issue

First of all let me tell you how I love this library. It is simple, powerful and made my work much, much better and enjoyable. Thanks mate!

To the point now.

I am having a case where I would like to call a certain function whenever I update the global state, but only when a particular property of the state has been changed.

I thought that the addCallback helper function could help me. The thing is that I cannot compare the new state with the previous one. Is it difficult to add the prevGlobal as the second parameter of the addCallback helper function?

Or maybe there is some other approach I did not think of?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
CharlesStovercommented, May 4, 2021

@monteiz Would you be able to unblock yourself with something like this?

let previousState = {};
addCallback((newState, _, stateChange) => {
  // Check state difference here.
  for (const [changedKey, newValue] of Object.entries(stateChange)) {
    console.log(`${changedKey}: ${previousState[changedKey]} => ${newValue}`);
  }

  // Update previous state for future comparisons.
  previousState = newState;
});
1reaction
monteizcommented, Mar 6, 2021

I solved it.

Actually the addCallback helper function has an undocumented 2nd and a 3rd parameters, which are - respectively - the dispatcherMap and the stateChange.

With the stateChange parameter I could very easily achieve what I was after.

I am more and more amazed by the elegance of this library.

@CharlesStover if you want, I can submit a pull request with the documentation updated.

Read more comments on GitHub >

github_iconTop Results From Across the Web

CharlesStover/reactn: React, but with built-in global ... - GitHub
Use addReducer to add a reducer to your global state. The first parameter is the name of your reducer. You will access your...
Read more >
Provide custom back navigation - Android Developers
This usually allows Android to properly navigate to previous destinations when the Back button is pressed. However, there are a few cases ...
Read more >
Call Back [Cisco Unified Communications Manager ...
Perform this step to add CallBack softkey to template and configure the softkey using the Common Device Configuration or phone.
Read more >
React hooks: accessing up-to-date state from within a callback
I need to call setState to access the previous state. Even though I have no intention of setting the state. I feel like...
Read more >
msrestazure.azure_operation.AzureOperationPoller class
Methods. add_done_callback. Add callback function to be run once the long running operation has completed - regardless of the status of the operation....
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