Skip to content

Props type mismatch in component

Quick summary

You passed a prop with the wrong type — the component expects a function but got an object, or expects a string but got a number.

Why this happens

  • The parent passes a value of a different type than the component expects (e.g., string instead of number)

  • An optional prop is not passed and the component does not handle the undefined case

  • A callback prop is passed a value instead of a function, or vice versa

Minimal example

✗ Broken code
function Price({ value, currency }) {
  return <span>{value.toFixed(2)} {currency}</span>;
}
// Parent passes string instead of number
<Price value='99.99' currency='USD' /> // value.toFixed is not a function
✓ Fixed code
function Price({ value, currency }) {
  return <span>{Number(value).toFixed(2)} {currency}</span>;
}
<Price value='99.99' currency='USD' /> // converts to number first

Converting the value to a number inside the component ensures toFixed is always available, regardless of the prop type passed.

How to diagnose

  • Use TypeScript or PropTypes to catch type mismatches at build time or runtime

  • Log the props inside the component to inspect their actual types at runtime

  • Check the component's prop documentation or type definition for the expected types

How to fix

  • Pass the correct type from the parent: convert values with String(), Number(), or wrap in a function

  • Add default values for optional props so the component handles undefined gracefully

  • Use TypeScript interfaces or PropTypes to define and enforce the expected prop types

How to prevent

  • Define prop types with TypeScript or PropTypes and enable strict type checking

  • Document the expected prop types and provide default values for optional props

Related Resources

Related Glossary

Related Lessons

← Back to language