New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

react18-use

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react18-use

React 19 use hook shim

latest
Source
npmnpm
Version
0.4.1
Version published
Weekly downloads
20K
31.16%
Maintainers
1
Weekly downloads
 
Created
Source

react18-use

CI npm size discord

React 19 use hook shim

Motivation

While waiting for React 19, I still want to release a library that depends on React 19 use hook. Hense, this shim.

It also provides useMemo + use(Context) experimental feature, which we hope to have in React 19. (See: https://x.com/TkDodo/status/1741193371283026422 )

Install

npm install react18-use

Usage

Promise

It works both in React 18 and React 19. However, you don't need it if you are using React 19.

import { Suspense, useState } from 'react';
import { use } from 'react18-use';

const Counter = ({ countPromise }: { countPromise: Promise<number> }) => {
  const count = use(countPromise);
  return <p>Count: {count}</p>;
};

const App = () => {
  const [countPromise, setCountPromise] = useState(Promise.resolve(0));
  return (
    <div>
      <button
        onClick={() =>
          setCountPromise(async (prev) => {
            await new Promise((resolve) => setTimeout(resolve, 1000));
            return (await prev) + 1;
          })
        }
      >
        +1
      </button>
      <Suspense fallback={<p>Loading...</p>}>
        <Counter countPromise={countPromise} />
      </Suspense>
    </div>
  );
};

Context

It works both in React 18 and React 19.

import { useState } from 'react';
import type { ReactNode } from 'react';
import { createContext, use, useMemo } from 'react18-use';

const MyContext = createContext({ foo: '', count: 0 });

const Component = () => {
  const foo = useMemo(() => {
    const { foo } = use(MyContext);
    return foo;
  }, []);
  return (
    <p>
      Foo: {foo} ({Math.random()})
    </p>
  );
};

const MyProvider = ({ children }: { children: ReactNode }) => {
  const [count, setCount] = useState(1);
  return (
    <div>
      <button onClick={() => setCount((c) => c + 1)}>{count}</button>
      <MyContext.Provider value={{ foo: 'React', count }}>
        {children}
      </MyContext.Provider>
    </div>
  );
};

const App = () => (
  <MyProvider>
    <Component />
  </MyProvider>
);

Limitations

  • Only supports promises and contexts.
  • It might not work exactly the same as React 19.
  • useMemo with use(Context) is experimental (feedback welcome).

Examples

The examples folder contains working examples. You can run one of them with

PORT=8080 pnpm run examples:01_counter

and open http://localhost:8080 in your web browser.

You can also try them directly: 01 02

Tweets

Keywords

react

FAQs

Package last updated on 19 May 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts