Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Sign InSign Up

Softans

Softans Logo Softans Logo
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
Home/ Questions/Q 3710
Jesson Holder
Jesson Holder
Asked: October 15, 20222022-10-15T09:52:45+00:00 2022-10-15T09:52:45+00:00

The state does not switch after the execution of the function

On my site there is a list of devices with their brief description (made in the form of a table). Information about devices is stored in the firestore.

The user can delete any device, respectively, the device will not be displayed to the user and will be deleted from the database (firestore)

Removing the device is as follows:

  1. the user clicks on the delete button;
  2. a window opens (something similar to a modal window made using Dialog mui) with a warning about the danger of deletion and two buttons: cancel and confirm;

    2a) when you click on the cancel button, the window closes and you can continue to work on the site;

    2b) pressing the confirmation button starts the deletion process, which is accompanied by a scroll wheel. After removing the device, the window closes and a notification should appear that the operation was completed successfully.

With the appearance of a notification that the operation was completed successfully, I had serious problems.

What am I doing:

  1. I created the state const[notificationShown, setNotificationShown] = useState(false);
  2. Further I switch this state setNotificationShown(true);
  3. And if notificationShown it is true, then I display the inscription about the successful removal.

But for some reason, notificationShown never switches to true, and accordingly, a record of successful deletion is not displayed.

My suspicion is that it has something to do with await deleteItem(item, firestore, urlParams); Let me explain: I put await before deleteItem, since the size of the file that contains information about the device is always different and, accordingly, the deletion time will always be different

export function DeleteButton({ item }) {
  const { urlParams } = useContext(PageContext)
  const { firestore } = useContext(AppContext)

  const [alertShown, setAlertShown] = useState(false);
  const [alertLoading, setAlertLoading] = useState(false);

  // The state that is responsible for notification of successful deletion
  const [notificationShown, setNotificationShown] = useState(false);


  const onNo = () => {
    setAlertShown(false);
  };

  const onYes = async () => {
    setAlertLoading(true);

    try {
      // Removes an entry from the firestore
      await deleteItem(item, firestore, urlParams);
      
      // Should toggle notificationShown to true and 
      // use setTimeout to hold the notification for two seconds
      setNotificationShown(true);
      setTimeout(() => {
        setNotificationShown(false);
      }, 2000); 

      setAlertShown(false);
    }  finally {
      setAlertLoading(false);
    }
  };

  
  return <ButtonGroup >

            {/* Responsible for the appearance of an inscription about the successful removal */}
            {notificationShown && (<span>The item was removed successfully</span>)} 

            <div onClick={() => setAlertShown(true)}>
                  <DeleteForeverIcon sx={{color: "#ff5141", marginRight: "5px"}}/>
              </div>

            {alertShown && (
                <Dialog open={onYes}>

                    <DialogTitle>Do you want to delete record?</DialogTitle>

                    <DialogContentText>
                       record will be removed from firestore
                    </DialogContentText>

                    {alertLoading 
                        ? <div>
                            <LabledSpinner/>
                          </div>
        
                        : <DialogActions>
                            <Button   onClick={onNo}>Cancel</Button >
                            <Button   onClick={onYes}>Confirm </Button >
                          </DialogActions>}
                     
                                       
                </Dialog>
                )}        
    </ButtonGroup>
}

Please help me figure out what could be the reason.

  • javascript
  • reactjs
  • async-await
the state does not switch after the execution of the function
  • 0
  • 0 0 Answers
  • 22 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • Popular
  • Answers
  • Ghulam Nabi

    Why are the British confused about us calling bread rolls ...

    • 5 Answers
  • Jerry

    Add file to native target programmatically via tuist/XcodeProj

    • 4 Answers
  • Ghulam Nabi

    Is this statement, “i see him last night” can be ...

    • 4 Answers
  • Ghulam Nabi
    Ghulam Nabi added an answer To resolve the NullPointerException, you need to identify the variable… March 15, 2023 at 8:25 am
  • Ghulam Nabi
    Ghulam Nabi added an answer You can replace the PnP code in your Azure Function… February 13, 2023 at 7:11 am
  • Ghulam Nabi
    Ghulam Nabi added an answer You can use the $match stage in the aggregate pipeline… February 10, 2023 at 6:20 am

Trending Tags

android c++ cypress flutter java javascript python selenium testng webdriver

Top Members

Robert

Robert

  • 3 Questions
  • 1k Points
Luci

Luci

  • 5 Questions
  • 1k Points
Kevin O Brien

Kevin O Brien

  • 2 Questions
  • 1k Points

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Softans

Softans is a social questions & Answers Engine which will help you establish your community and connect with other people.

About Us

  • Blog
  • Jobs
  • About Us
  • Meet The Team
  • Contact Us

Legal Stuff

Help

Follow

© 2021 Softans. All Rights Reserved
With Love by Softans.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.