Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Conversation

@beyackle
Copy link
Contributor

Description

This adds screen-reader text to the loading-spinner box in the package manager, giving SR-only users feedback that they've successfully gotten a package to be installed or removed.

Task Item

refs #6559

Comment on lines 20 to 25
useEffect(() => {
async function doAnnouncement() {
announce(props.title);
}
doAnnouncement();
}, [props.title]);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: this does look strange, but it's the correct way to use an async function inside useEffect.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitely seems incorrect though. What's wrong with this?

useEffect(() => {
  announce(props.title);
}, [props.title]);

If you wanted to await something, that would be the way to do it, but just invoking an async function shouldn't require doing this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it shouldn't, but useEffect has a restriction where it doesn't like something that returns a promise. This is a workaround documented in places like https://www.robinwieruch.de/react-hooks-fetch-data and facebook/react#14326 (where Dan Abramov himself weighs in).

Copy link
Contributor

@hatpick hatpick Mar 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't await a promise within a useEffect block, but you can just call it (like Andy says above)

(use callbacks to handle results if required OR use IIFE to await it like:

(async () => {
   await yourAsyncFunction(args);
})();

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the type definition, announce isn't async.

announce: (message: string) => void;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it isn't, but trying to do this the obvious way makes the app crash with the same error that blog post mentions: Warning: useEffect function must return a cleanup function or nothing. Promises and useEffect(async () => ...) are not supported, but you can call an async function inside an effect. Pretending it's async and using the IIFE workaround makes this work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a misunderstanding here @beyackle

  1. is the type definition for announce wrong and it actually does return a promise?
  2. Even if it does return a promise, it is still ok to invoke an async function inside an effect without using await. (First example I found, but there are others)

@hatpick and I are just saying that in this case, the IIFE is unnecessary because we do not need to await the function.

Comment on lines 20 to 25
useEffect(() => {
async function doAnnouncement() {
announce(props.title);
}
doAnnouncement();
}, [props.title]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitely seems incorrect though. What's wrong with this?

useEffect(() => {
  announce(props.title);
}, [props.title]);

If you wanted to await something, that would be the way to do it, but just invoking an async function shouldn't require doing this.

@coveralls
Copy link

coveralls commented Mar 30, 2021

Coverage Status

Coverage remained the same at 51.4% when pulling 4dd97b4 on beyackle/announceInsidePackageManager into 31d4ca5 on main.

@beyackle beyackle merged commit 99d7e4c into main Apr 2, 2021
@beyackle beyackle deleted the beyackle/announceInsidePackageManager branch April 2, 2021 16:05
@cwhitten cwhitten mentioned this pull request May 20, 2021
lei9444 pushed a commit to lei9444/BotFramework-Composer-1 that referenced this pull request Jun 15, 2021
* Update WorkingModal.tsx

* Update WorkingModal.tsx

* Update WorkingModal.tsx

* Update WorkingModal.tsx

* remove unneeded async wrapper

Co-authored-by: Andy Brown <[email protected]>
Co-authored-by: TJ Durnford <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants