I'm trying to learn and some of the unreleased "unstable" features of React. In particular, I want to use the "useEffectEvent" hook.
The problem is that I can't import that part of the package in any way.
When I try to import something from the React official package in this way:
import * as React from "react"
The module automatically points to this directory (the @types/react npm package)
node_modules/@types/react/index"
The experimental hooks (such as "React.experimental_useEffectEvent") are here though:
node_modules/react/index"
This happens both with CRA and Vite, and I tried with standard JS builds (no TS, no tsconfig file).
How can I reach that part of React's code? Thanks
There are two (or three) aspects of this (both shown in the documentation, though #2 is a bit subtle [it's at the top, but not in the code sample]):
react@experimental and react-dom@experimental modules.experimental_useEffectEvent, not just useEffectEvent.So:
import { experimental_useEffectEvent } from "react";
// ...
experimental_useEffectEvent(/*...*/);
or perhaps (for now):
import { experimental_useEffectEvent as useEffectEvent } from "react";
// ...
useEffectEvent(/*...*/);
Codesandbox example
If you're using TypeScript, you'll want to read the instructions in the @types/react module's experimental.d.ts file to see how to get TypeScript to recognize the experimental hooks. There are a couple of different ways; one of the simpler ones is to add this to any file in the project (I used App.tsx):
/// <reference types="react/experimental" />
FWIW, here's how I bootstrapped a new project using the experimental features:
npm create vite@latest (I use Vite.js as my bundler).
npm install react@experimental react-dom@experimentalApp.tsx with:
/// <reference types="react/experimental" />
import { experimental_useEffectEvent as useEffectEvent } from "react";
import "./App.css";
function App() {
return <div>typeof useEffectEvent: {typeof useEffectEvent}</div>;
}
export default App;
When I run that, I get typeof useEffectEvent: function on the page.
I don't use create-react-app, but I did briefly try it, but ran into dependency issues upgrading React to the experimental version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With