[keep-awake] Add a flag to enable or disable useKeepAwake. #28908
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Why
useKeepAwake is a nice declarative interface for activating KeepAwake for as long as a component is on-screen. Great!
Now, let's only keep the screen awake when in a particular state, e.g. while a video or animation is playing.
We can always wrap the hook in a component to get around this, but it's not very ergonomic:
What if we could do something like this instead?
How
Let's add a boolean option
enabled
to useKeepAwake that allows for the effect to be disabled. If the caller doesn't provide this option, the the effect is always active (the same as the current behaviour).When the enabled option is exactly
false
, we simply don't do anything in theuseEffect
.Side note
The resulting interface is still a little bit awkward since the user still needs to pass the tag parameter, which should most often be
undefined
:It'd be nice to ad an overload allowing for the options object to be the first parameter. I doubt that providing
tag
is more frequent than providingoptions
especially after #28884. Personally, I'd movetag
inside the options parameter and deprecate the current call signature, but let's think about that later.Test Plan
enabled
istrue
orundefined
/absent, and not activated whenenabled
is false.enabled
changes for subsequent re-renders.Checklist
npx expo prebuild
& EAS Build (eg: updated a module plugin).