npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-keep-awake

v1.1.1

Published

React component and hook to keep screen awake.

Readme

React keep awake npm npm type definitions

React component and hook to keep screen awake using Screen Wake Lock API.

Installation

npm install react-keep-awake

How to use

To keep screen awake by component:

import { KeepAwake } from 'react-keep-awake'

const MyApp = () => {
	return (
		<main>
			<KeepAwake />
			<p>Lorem ipsum</p>
		</main>
	)
}

To keep screen awake by hook:

import { useKeepAwake } from 'react-keep-awake'

const MyApp = () => {
	useKeepAwake()

	return (
		<main>
			<p>Lorem ipsum</p>
		</main>
	)
}

Knowing whether it worked

A screen wake lock is unavailable in more places than it is available — not in any WebView, and the browser can refuse one on a device that is low on battery. The hook reports what actually happened, so silence is not mistaken for success:

const { isSupported, isActive, error } = useKeepAwake()
  • isSupported — whether this environment can keep the screen awake at all.
  • isActive — whether the screen is being held awake at this moment. The browser hands the lock back whenever the page is hidden, so this goes false and true again on its own.
  • error — why the last attempt failed, or null.

Keeping the screen awake some other way

Where the Screen Wake Lock API is missing — a web app inside a native shell, say — supply your own strategy while the app starts:

import { setKeepAwakeStrategy } from 'react-keep-awake'

setKeepAwakeStrategy({
	isSupported: () => Boolean(window.ReactNativeWebView),
	activate: ({ onActiveChange, onError }) => {
		// Listen first: the shell is the one that knows whether it got the
		// hold, and it can lose it later without anyone asking the web.
		const stopListening = listenToNative((message) => {
			if (message.type === 'keepAwakeChanged') {
				onActiveChange(message.isActive)
			} else if (message.type === 'keepAwakeFailed') {
				onError(message.reason)
			}
		})

		postMessageToNative({ type: 'keepAwakeStart' })

		return () => {
			postMessageToNative({ type: 'keepAwakeStop' })
			stopListening()
		}
	},
})

activate is called for the first component asking to keep the screen awake and its result is called once the last one goes away, so a strategy never sees the counting. useKeepAwake reports its state the same way it reports the built-in one.

Call it before the first component asks for the screen — claims are counted per instance, so the strategy cannot be swapped afterwards.

A strategy that throws is treated exactly like one that reports an error: it is recorded in error and the app carries on. Keeping the screen awake is a comfort, never a reason to bring down what asked for it.

For a second, separately counted instance — a test, or a story — build one with createKeepAwake(strategy) and drive it yourself.

Development

npm ci
npm run dev

Run the tests with npm test.

Releasing

npm version minor   # or patch, or major
git push --follow-tags

Pushing the tag is the release: CI runs the checks and publishes to npm. It authenticates as a trusted publisher over OpenID Connect, so no npm token is kept in the repository — which also means the workflow file has to keep the name npmjs.com was told about.