This error occurs when React.forwardRef cannot be imported or called properly, typically due to incorrect React imports, version mismatches between react and react-dom, or using incompatible library versions that don't support forwardRef.
The "forwardRef is not a function" error indicates that the React.forwardRef API is either undefined or not available in the current environment. This is a runtime error that prevents components from using ref forwarding functionality. forwardRef is a React API introduced in React 16.3 that allows components to pass refs through to child components. When this error occurs, it means the forwardRef function is not accessible, which can happen for several reasons including incorrect imports, version mismatches, or attempting to use forwardRef with libraries that don't support it (like older versions of Preact). The error typically manifests when trying to create a ref-forwarding component or when third-party libraries that use forwardRef internally cannot access the API properly.
Check your package.json to ensure react and react-dom have identical versions:
{
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
}If versions don't match, update them to the same version:
npm install [email protected] [email protected]
# or
yarn add [email protected] [email protected]Delete node_modules and lockfile, then reinstall:
rm -rf node_modules package-lock.json
npm installVerify you're importing forwardRef correctly from React:
// Correct - named import from 'react'
import { forwardRef } from 'react';
// Also correct - accessing from React namespace
import React from 'react';
const MyComponent = React.forwardRef((props, ref) => {
// component code
});Common incorrect imports to avoid:
// WRONG - forwardRef is not in react-dom
import { forwardRef } from 'react-dom';
// WRONG - typo in import name
import { forwardRef } from 'raect';Multiple React instances can cause this error. Run this command to check:
npm ls react
# or
yarn why reactIf you see multiple versions, deduplicate them:
# npm
npm dedupe
# yarn
yarn dedupe
# pnpm
pnpm dedupeFor webpack projects, add this to your webpack.config.js:
module.exports = {
resolve: {
alias: {
react: require.resolve('react'),
'react-dom': require.resolve('react-dom')
}
}
};forwardRef was introduced in React 16.3. Check your React version:
npm list reactIf using an older version, upgrade to at least React 16.3:
npm install react@^16.3.0 react-dom@^16.3.0
# or for the latest
npm install react@latest react-dom@latestNote: React 19 no longer requires forwardRef - you can pass ref as a regular prop in function components.
If using Preact as a React alternative, verify you're on Preact X (10.x or higher):
npm list preactPreact 8.x does not support forwardRef. Upgrade to Preact X:
npm install preact@latest preact-compat@latestUpdate your webpack aliases for Preact X:
// webpack.config.js
module.exports = {
resolve: {
alias: {
react: 'preact/compat',
'react-dom': 'preact/compat'
}
}
};Some libraries had forwardRef compatibility issues in older versions. Update them:
# Check for outdated packages
npm outdated
# Update specific libraries known to have issues
npm update @reach/ui mobx-react react-router-domIf a specific library version is causing issues, check its changelog and upgrade to a version with forwardRef support.
If the error occurs only in Jest tests, update your Jest configuration:
// jest.config.js
module.exports = {
testEnvironment: 'jsdom',
moduleNameMapper: {
'^react$': require.resolve('react'),
'^react-dom$': require.resolve('react-dom')
},
// Ensure all packages use the same React version
moduleDirectories: ['node_modules', '<rootDir>/']
};Also ensure your test setup uses the correct React version:
npm install --save-dev @testing-library/react@latestReact 19 Changes: In React 19, forwardRef is no longer necessary for function components. You can access refs directly as props, which simplifies component code and eliminates this entire class of errors. If upgrading to React 19, consider refactoring forwardRef components to use ref as a standard prop.
Bundle Analysis: If you're experiencing this in production builds but not development, use webpack-bundle-analyzer or similar tools to check if multiple React versions are being bundled. This often happens when using monorepos or linking local packages.
Third-Party Type Validation: The typeof React.forwardRef(...) check returns 'object', not 'function', which can break third-party libraries that validate component types using typeof Component === 'function'. This is expected behavior, not a bug. Libraries should use React.isValidElement or check for React.$$typeof instead.
Monorepo Considerations: In monorepo setups (Yarn workspaces, Lerna, pnpm workspaces), symlinked packages can cause multiple React instances. Use the workspace's hoisting mechanism or explicitly configure bundlers to resolve to a single React instance at the root level.
Prop spreading could cause security issues
Prop spreading could cause security issues
Error: error:0308010C:digital envelope routines::unsupported
Error: error:0308010C:digital envelope routines::unsupported
React Hook "useEffect" is called conditionally. React Hooks must be called in the exact same order in every component render.
React Hook useEffect placed inside a condition
Hook can only be called inside the body of a function component
Hook can only be called inside the body of a function component
Rollup failed to resolve import during build
How to fix "Rollup failed to resolve import" in React