How to fix Component should be a function, class or ForwardRef, not a string in React

ReactINTERMEDIATEHIGHUpdated Dec 9, 2025· 112 views

React throws this error when you pass a string value where a component is expected. This commonly occurs when importing components incorrectly, passing component names as strings, or when dynamic component resolution fails.

Quick fix

Fixes Component should be a function, class or ForwardRef, not a string

// Component file
export const MyComponent = () => <div>Hello</div>;

// Usage file
import { MyComponent } from './MyComponent';
<MyComponent />
  1. Check your component import statements
  2. Verify the component is actually exported
  3. Fix dynamic component rendering
  4. Check for circular dependencies
react — error
// Component file
export const MyComponent = () => <div>Hello</div>;
// Usage file
import { MyComponent } from './MyComponent';
<MyComponent />
Component should be a function, class or ForwardRef, not a string

What this error means

This error occurs when React's component validation detects that you're trying to render something that looks like a component but is actually a string value. React expects components to be one of three things: a function component, a class component, or a ForwardRef component. When you accidentally pass a string instead of the actual component reference, React cannot render it and throws this error. This usually happens during the JSX transformation process. When you write JSX like <MyComponent />, React internally calls React.createElement(MyComponent). If MyComponent is a string rather than an actual component, React's validation fails immediately. The error is React's way of protecting you from runtime issues. If React tried to render a string as if it were a component, it would cause unpredictable behavior or crashes. Instead, it fails fast with a clear error message indicating exactly what type was expected versus what was received.

Typical symptoms

  • Application crashes immediately when trying to render a specific component
  • Error appears in console: "Component should be a function, class or ForwardRef, not a string"
  • Component renders as blank or missing in the UI
  • Error occurs after refactoring imports or component structure
  • Dynamic component rendering fails with type validation error
  • Tests fail with component type validation errors

Common causes

  1. Passing a component name as a string literal instead of the component reference
  2. Incorrect import statement (mixing up default and named exports)
  3. Component variable is undefined due to circular dependency or missing export
  4. Dynamic component resolution returns a string instead of the component
  5. Storing component names as strings in state or configuration without proper mapping
  6. Incorrect destructuring of component imports
  7. Using string values in component mapping objects instead of actual component references
  8. Accidentally wrapping component reference in quotes

How to fix "Component should be a function, class or ForwardRef, not a string"

1Check your component import statements

Verify that you're importing the component correctly. The most common issue is mixing up default and named exports.

Named export (correct):

jsx
// Component file
export const MyComponent = () => <div>Hello</div>;

// Usage file
import { MyComponent } from './MyComponent';
<MyComponent />

Default export (correct):

jsx
// Component file
const MyComponent = () => <div>Hello</div>;
export default MyComponent;

// Usage file
import MyComponent from './MyComponent';
<MyComponent />

Incorrect (causes error):

jsx
// Component file uses named export
export const MyComponent = () => <div>Hello</div>;

// Usage file tries default import
import MyComponent from './MyComponent'; // Returns undefined!
<MyComponent /> // Error: undefined is not a valid component
2Verify the component is actually exported

Make sure the component is properly exported from its source file.

Check the export:

jsx
// MyComponent.tsx
const MyComponent = () => {
  return <div>Content</div>;
};

// Missing export! This will cause import to fail
// export default MyComponent; // Add this line

Verify in usage file:

jsx
import MyComponent from './MyComponent';

// Log to check what was imported
console.log('MyComponent:', MyComponent);
// Should show: function or class, not undefined or string

<MyComponent />
3Fix dynamic component rendering

If you're dynamically selecting components, ensure you're mapping to actual component references, not strings.

Incorrect (causes error):

jsx
const componentMap = {
  header: 'Header',  // String, not component!
  footer: 'Footer'
};

const ComponentToRender = componentMap[type];
<ComponentToRender /> // Error!

Correct:

jsx
import Header from './Header';
import Footer from './Footer';

const componentMap = {
  header: Header,  // Actual component reference
  footer: Footer
};

const ComponentToRender = componentMap[type];
<ComponentToRender /> // Works!
4Check for circular dependencies

Circular dependencies can cause imports to resolve to undefined at runtime.

Identify circular dependency:

bash
# Use a tool like madge to detect cycles
npx madge --circular src/

Example of circular dependency:

jsx
// ComponentA.tsx
import ComponentB from './ComponentB';
export const ComponentA = () => <ComponentB />;

// ComponentB.tsx
import ComponentA from './ComponentA'; // Circular!
export const ComponentB = () => <ComponentA />;

Fix by extracting shared dependencies:

jsx
// SharedTypes.ts (new file)
export interface SharedProps { /* ... */ }

// ComponentA.tsx
import { SharedProps } from './SharedTypes';
import ComponentB from './ComponentB';

// ComponentB.tsx
import { SharedProps } from './SharedTypes';
// Don't import ComponentA
5Verify ForwardRef usage

If using React.forwardRef, ensure you're returning it correctly.

Incorrect:

jsx
const MyComponent = React.forwardRef((props, ref) => {
  return 'div'; // String instead of JSX!
});

Correct:

jsx
const MyComponent = React.forwardRef((props, ref) => {
  return <div ref={ref}>{props.children}</div>;
});

export default MyComponent;
6Check React.lazy and Suspense usage

When using code-splitting with React.lazy, ensure the dynamic import returns a valid component.

Incorrect:

jsx
const LazyComponent = React.lazy(() =>
  import('./MyComponent').then(module => module.componentName) // Returns string!
);

Correct:

jsx
// Default export
const LazyComponent = React.lazy(() => import('./MyComponent'));

// Or named export
const LazyComponent = React.lazy(() =>
  import('./MyComponent').then(module => ({ default: module.MyComponent }))
);

// Usage with Suspense
<Suspense fallback={<div>Loading...</div>}>
  <LazyComponent />
</Suspense>

Advanced notes

TypeScript considerations:

If you're using TypeScript, you can prevent many of these errors at compile time with proper typing:

typescript
import { ComponentType } from 'react';

// Type-safe component mapping
const componentMap: Record<string, ComponentType<any>> = {
  header: Header,
  footer: Footer
};

// TypeScript will error if you try to add a string
// componentMap.invalid = 'NotAComponent'; // Type error!

Debugging in development:

React DevTools won't show much for this error since the component never renders. Instead:

1. Add console.log statements before the failing render
2. Check the Network tab to ensure component files are loading
3. Use breakpoints in your bundler's source maps to inspect imported values

Common framework-specific issues:

- Next.js: Server Components cannot use React.forwardRef. Convert to Client Component with 'use client' directive
- Remix: Check that your component exports match the framework's conventions (default vs named)
- Vite: Ensure your JSX pragma is configured correctly in vite.config.ts

Performance impact:

While this error prevents rendering entirely, be aware that having many dynamic component mappings can impact bundle size. Consider code-splitting with React.lazy for large component libraries.

Related React internals:

React's createElement function performs type validation before creating element objects. The valid types are:
- String (built-in HTML tags like 'div', 'span')
- Function (function components)
- Class (class components extending React.Component)
- Symbol (React.Fragment, React.StrictMode)
- ForwardRef object (from React.forwardRef)

Any other type, including plain strings that aren't HTML tag names, will fail validation.

Frequently asked questions

What does "Component should be a function, class or ForwardRef, not a string" mean?
This error occurs when React's component validation detects that you're trying to render something that looks like a component but is actually a string value. React expects components to be one of three things: a function component, a class component, or a ForwardRef component. When you accidentally pass a string instead of the actual component reference, React cannot render it and throws this error.
How do I fix "Component should be a function, class or ForwardRef, not a string"?
React throws this error when you pass a string value where a component is expected. This commonly occurs when importing components incorrectly, passing component names as strings, or when dynamic component resolution fails.
What causes "Component should be a function, class or ForwardRef, not a string"?
Passing a component name as a string literal instead of the component reference Incorrect import statement (mixing up default and named exports) Component variable is undefined due to circular dependency or missing export
Is the "Component should be a function, class or ForwardRef, not a string" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading