How to fix forwardRef is not a function in React
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.
Quick fix
Fixes forwardRef is not a function
{
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
}- Verify React and ReactDOM versions match
- Check your forwardRef import statement
- Check for multiple React instances
- Verify React version supports forwardRef
{"dependencies": {"react": "^18.2.0","react-dom": "^18.2.0"}}forwardRef is not a function
What this error means
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.
Typical symptoms
- TypeError: forwardRef is not a function when importing from React
- Components using forwardRef fail to render at runtime
- Third-party component libraries throw errors on initialization
- Build completes successfully but application crashes in browser
- Error occurs specifically with ref-forwarding components
- Jest tests fail with "React.forwardRef is not a function"
Common causes
- Version mismatch between react and react-dom packages
- Incorrect import statement (importing from wrong package or path)
- Using Preact 8.x which doesn't support forwardRef (requires Preact X/10.x+)
- Webpack alias configuration pointing to incompatible React version
- Multiple React instances loaded in the same application
- Using React version older than 16.3 where forwardRef doesn't exist
- Outdated third-party libraries that have forwardRef compatibility issues
- Bundler configuration issues that incorrectly resolve React modules
How to fix "forwardRef is not a function"
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 react@18.2.0 react-dom@18.2.0
# or
yarn add react@18.2.0 react-dom@18.2.0Delete 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@latestAdvanced notes
React 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.
Frequently asked questions
- What does "forwardRef is not a function" mean?
- 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. 3 that allows components to pass refs through to child components.
- How do I fix "forwardRef is not a function"?
- 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.
- What causes "forwardRef is not a function"?
- Version mismatch between react and react-dom packages Incorrect import statement (importing from wrong package or path) Using Preact 8.x which doesn't support forwardRef (requires Preact X/10.x+)
- Is the "forwardRef is not a function" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.