Skip to content
Advertisement

Can’t update state to change display on page during api call

I have a page called Dashboard and on this page I have a component called Dropzone which is used for uploading a file to the page.

When a file is uploaded, it calls the onDrop() callback where I POST the file to my api, and I then try to sent a GET request to my api at /api/machines/{hash}. I want to set my state.pcapAnalysing variable to TRUE before this GET request, and set it to FALSE after it finishes. The idea of this is to show an ‘analysing’ message on the Dashboard page while the api call is running, this is likely to take a few seconds to return.

Due to the state not being refreshed, the message does not show as the state never updates to show state.pcapAnalysing as anything but false. Does anyone have any idea how I can achieve the effect I’m looking for? Essentially, I’m trying to show a message on the Dashboard page for the duration of the api call action, but have this triggered by the component residing in Dropzone.js. Thanks.

Dashboard.js

JavaScript

Dropzone.js

JavaScript

Advertisement

Answer

Looks like you should be setting the final state within the then close of your /api/machines call. Not after it.

So the whole call should look like this:

JavaScript

Notice I removed that line altogether

JavaScript

Because even if I moved it within the then close, the state wouldn’t have been updated yet, because the component re-render hasn’t occurred yet.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement