Poll and update state UNLESS a specific prop updates

I’m building a React Native application that is polling for wallet balances every 6 seconds. I’ve setup a timer which I am using to 1) call ONCE initially for balance (ie. not wait 6 seconds for the interval in my timer to run) and 2) poll as normal after that.

The problem I’m having is the user can change the state while the polling is happening but because the interval is already running what happens is the user updates the state then the polling code overwrites it.

For example given assetBalances of:

{ lorem: { balance: 100 } }

If the user were to change the wallet name from "lorem" to "ipsum" I’d momentarily be left with assetBalances of:

{ ipsum: { balance: 100 } }

Which would then quickly become the following when the poll has ran. (The reason it adds to the object is because I spread with ... as you can see in network-scan.ts):

{ lorem: { balance: 100 }, ipsum: { balance: 100 } }

I’ve tried using a custom usePrevious hook which compares the last prop to current prop and then I conditionally call the setAsyncValue in network-scan.ts but that didn’t work.

I need a way to CANCEL the running timer.start instance if x prop changes (x being assetBalances in my first case).

On a side note I’m also having an issue where for some reason when there’s lots of state changes the timer gets called multiple times which causes loads of intervals to run in parallel, which I don’t understand because I’m calling timer.stop() at the start of every useEffect re-render – why?

If anyone has any ideas as to how I can solve these issues I’d really appreciate it! Or is there a much easier way to do what I’m trying to do?

Here’s some code for context:


const timer = {
  running: false,
  timeout: false,
  cb: function () {},
  start: function (cb: any, iv: number, cbAtEnd?: any) {
    const elm = this;
    this.running = true;
    if (cb) this.cb = cb;
    this.timeout = setTimeout(function () {
      if (cbAtEnd) cbAtEnd();
    }, iv);
  execute: function (e: any) {
    if (!e.running) return false;
  stop: function () {
    this.running = false;


export default function useNetworkPolling(): void {
  const {
    state: {
  } = useContext(LocalContext);

  const intervalTime = networkPollingInterval // default 6s
  const address = useGetWalletAddress(selectedWallet);

  useEffect(() => {
    function scan() {
      console.log(`Scan now! - (Interval time is: ${intervalTime}ms) [from useNetworkPolling]`);
      const filteredTokens = getTokensFilteredByTrackedStatus(
      const allTokens = CORE_TOKENS.concat(filteredTokens);

      console.log(JSON.stringify(JSON.parse(assetBalances), null, 2));

    timer.stop(); // Make sure the previous one is stopped so two never run at the same time

    if (userToken && JSON.parse(wallets).length) {
      // Start off with an immediate one-off call
      timer.start(scan, 1, () => {
        timer.stop(); // Pass a callback into the start function which stops the first timer
        timer.start(scan, intervalTime); // Start polling with the interval
  }, [


export default async function handleNetworkScan(
  coreAndTrackedTokens: Token[],
  assetBalances: any,
  address: string,
  setAsyncValue: (key: string, value: string) => Promise<void>,
  connectedNetwork: string,
  selectedWallet: string
) {
  let updatedBalances = assetBalances[selectedWallet];

  for (const token of coreAndTrackedTokens) {
    try {
      // Redacted logic that updates updatedBalances if success...
    } catch (e) {
      // Redacted logic that updates updatedBalances if error...

    JSON.stringify({ ...assetBalances, [selectedWallet]: updatedBalances })

121 thoughts on “Poll and update state UNLESS a specific prop updates”

  1. Wow, awesome blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your site
    is fantastic, let alone the content!

  2. Nice post. I was checking continuously this blog and I am
    impressed! Very helpful information particularly the remaining section 🙂 I take care of such information a lot.

    I used to be seeking this particular information for a very lengthy time.
    Thank you and good luck.

  3. I am really enjoying the theme/design of
    your weblog. Do you ever run into any browser compatibility issues?

    A handful of my blog readers have complained about my website not working correctly
    in Explorer but looks great in Opera. Do you have any suggestions to help fix this problem?

  4. I think this is one of the most vital information for me.

    And i’m glad reading your article. But wanna remark on few general things,
    The web site style is perfect, the articles is really nice :
    D. Good job, cheers

  5. Howdy! I could have sworn I’ve been to this blog before but after
    browsing through many of the posts I realized it’s new to me.

    Anyhow, I’m certainly pleased I found it and I’ll be bookmarking it and checking back regularly!

  6. I am curious to find out what blog platform you happen to be
    working with? I’m having some small security issues
    with my latest website and I’d like to find
    something more safeguarded. Do you have any

  7. Hi there! I could have sworn I’ve been to this website before but after going through some of the posts I realized it’s new to me.
    Regardless, I’m definitely happy I discovered it
    and I’ll be bookmarking it and checking back regularly!

  8. Hey there I am so thrilled I found your blog, I really found you by error, while I was browsing on Aol
    for something else, Nonetheless I am here now and would just like to say thanks for a tremendous post and a
    all round enjoyable blog (I also love the theme/design),
    I don’t have time to look over it all at the
    minute but I have saved it and also included your RSS feeds, so when I have
    time I will be back to read a great deal more, Please do keep up the excellent work.

  9. I’m impressed, I have to admit. Seldom do I encounter a blog that’s both educative and interesting, and let
    me tell you, you’ve hit the nail on the head.

    The issue is something too few men and women are speaking intelligently about.
    Now i’m very happy I found this during my hunt for something regarding this.

  10. With havin so much content do you ever run into any problems of plagorism or copyright infringement?

    My site has a lot of exclusive content I’ve either authored myself or outsourced but it seems a lot
    of it is popping it up all over the internet without my agreement.

    Do you know any methods to help prevent content from being stolen?
    I’d truly appreciate it.

  11. Howdy would you mind letting me know which hosting company you’re utilizing?
    I’ve loaded your blog in 3 completely different internet browsers and I must say
    this blog loads a lot faster then most. Can you recommend a good hosting provider
    at a fair price? Many thanks, I appreciate it!

  12. Hey Thеre. I discovered your webⅼog the ᥙse of msn. Thiis іss a very smartly written аrticle.
    І will maake sure to Ьookmark iit and return to read more of your helpful info.

    Thanks for the post. I will ccertainly comeback.

    Also visit my blog post :: kinghoki4d

  13. Hey! This is kind of off topic but I need some guidance from an established blog.
    Is it tough to set up your own blog? I’m not very techincal but I can figure things out
    pretty fast. I’m thinking about setting up my own but I’m
    not sure where to begin. Do you have any tips or suggestions?
    Thank you

  14. Hi there! I know this is kinda off topic but I was wondering which blog platform
    are you using for this website? I’m getting sick and tired of WordPress because I’ve had problems with hackers and I’m looking at alternatives for another platform.
    I would be awesome if you could point me in the direction of a good platform.

  15. I’m impressed, I must say. Rarely do I come across a blog that’s equally educative and entertaining, and
    let me tell you, you’ve hit the nail on the head.
    The issue is something which too few people are speaking intelligently about.
    I’m very happy that I came across this in my search for something
    concerning this.

  16. Hey there are using WordPress for your site platform? I’m new to the blog world but I’m trying
    to get started and set up my own. Do you require any html coding knowledge to
    make your own blog? Any help would be really appreciated!

  17. I’d like to thank you for the efforts you have put in penning this site.
    I am hoping to view the same high-grade content by you later on as well.
    In fact, your creative writing abilities has inspired me to
    get my own, personal website now 😉

  18. Cɑn I just say what a гelief to discover a person that genuіnely understands what they’re
    tazlking about on the internet. Yоu definitely knoᴡ how to bring an іssue to light and make it іmⲣortant.
    More people need to ook at thіs and understand
    this side of your story. I was surprised that you’re not more pⲟpular because you
    most certainly haνe the gift. http://zcwbz.com/comment/html/?261099.html

  19. Ꮲretgtʏ еⅼement of content. I just stumbled upon your web site andd inn
    accession capital to assert that I gеt actually enjoyed accounnt your
    webglog pⲟsts. Anyway I’ll be sսbscribing tօ your feeds and even I achievement you get rigһt of entry to
    constantly quіckly.

    Fеel free too visit my web page :: link
    slot gacor hari inni pragmatic play (http://www.bausch.co.nz)

  20. Ꮋowdy thiѕ is somewhаt of off topіc but I was wondering if blogs
    use WYSIWYG edfitors or if you һave to manually ϲodе with HTML.
    I’m ѕtarting a blog soon but have no coding skills soo
    I wanted to get advic from someone with experіence.
    Any help would be greatly appreciated!

    My homepаge – link slot gacor deposit 10
    ribu (https://www.beacon-bookmarks.win/link-slot-gacor-2021-terpercaya-2022-1)

  21. Howdy I am so thrilled I found your blog, I really found you by accident, while I was looking on Aol for
    something else, Regardless I am here now and would just like to say thanks for a marvelous post and a all round interesting blog
    (I also love the theme/design), I don’t have time to go through it all at the minute but I have book-marked it and also added
    your RSS feeds, so when I have time I will be back to read
    a lot more, Please do keep up the great work.

  22. It is perfect time to make some plans for the future and it’s time to be happy.
    I have read this post and if I could I wish to suggest you few
    interesting things or tips. Maybe you can write next articles referring
    to this article. I wish to read even more things about it!

  23. I was curious if you ever considered changing the layout
    of your blog? Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content
    so people could connect with it better. Youve got an awful lot of text for
    only having one or 2 images. Maybe you could space it out better?

  24. Good day! This is kind of off topic but I need some advice from an established blog.
    Is it hard to set up your own blog? I’m not very techincal but I can figure things out pretty fast.

    I’m thinking about setting up my own but I’m not sure where to start.
    Do you have any points or suggestions? Cheers

  25. Have you ever considered writing an ebook or guest authoring on other websites?
    I have a blog based on the same topics you discuss and
    would really like to have you share some stories/information. I know my audience would value your
    work. If you are even remotely interested, feel free to send me an e mail.

  26. First of all I want to say great blog! I had a quick question which I’d like to ask if you don’t mind.
    I was interested to know how you center yourself and clear
    your mind before writing. I have had trouble
    clearing my mind in getting my ideas out. I truly do take pleasure in writing but it just seems like
    the first 10 to 15 minutes are usually lost simply just trying to figure
    out how to begin. Any ideas or tips? Kudos!

  27. What i don’t realize is in reality how you’re no
    longer actually a lot more well-favored than you might be now.
    You are so intelligent. You realize therefore significantly on the subject of this matter, produced me in my view
    believe it from numerous numerous angles. Its like men and women are not involved unless it’s something to do with Woman gaga!

    Your personal stuffs excellent. All the time handle it up!

  28. Hello would you mind stating which blog platform you’re working with?
    I’m going to start my own blog in the near future but I’m having a tough time making a decision between BlogEngine/Wordpress/B2evolution and Drupal.

    The reason I ask is because your layout seems different then most blogs and I’m looking for something
    unique. P.S My apologies for getting off-topic but I had to ask!

  29. Its like you read my mind! You seem to know so much about this, like you
    wrote the book in it or something. I think that you can do with some
    pics to drive the message home a bit, but other than that,
    this is great blog. A great read. I will definitely be back.


Leave a Comment