Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

A PropTypes warning says a required prop is undefined

ErrorWarning: Failed prop type: The prop `onSave` is marked as required in `Editor`, but its value is `undefined`.

Short answer

A component declared a prop with isRequired, and the parent passed nothing or undefined. The warning names the prop and the component. Find the parent that renders it and pass the value, or give the prop a default. In React 19 propTypes are ignored, so the same bug is no longer reported.

  • Applies to React 17, React 18, prop-types 15
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    The warning names a prop and a component. Search for where that component is rendered, then look at the line that should pass the prop.

    grep -rn "<Editor" src/
  2. 2Match what you see, then jump to its fix

    1. 1The warning appears on first render, for every use of the component.The parent never passes the prop, or passes a misspelled nameFix
    2. 2The warning shows only on the first render, and a later render has the value.The value is undefined at first, because it comes from data that is not loaded yetFix
    3. 3After the upgrade the warning no longer shows, but the prop is still missing.You moved to React 19 and the warnings disappearedFix

What does the error look like?

  • Warning: Failed prop type: The prop `onSave` is marked as required in `Editor`, but its value is `undefined`.

Cause 1 of 3

The parent never passes the prop, or passes a misspelled name

1Check

isRequired makes React show a warning if the prop isn't provided. A prop passed under another name counts as missing.

grep -rn "isRequired" src/components/Editor.jsx
grep -rn "<Editor" src/

2Fix

Pass the prop with the exact name the component declares.

<Editor onSave={handleSave} />

3Verify

Reload the page in development mode and check that the console is quiet.

npm run dev

Confirmed by github.com

Did this fix it?

Cause 2 of 3

The value is undefined at first, because it comes from data that is not loaded yet

1Check

A variable read from state, a query result or a route parameter is undefined until it arrives. Passing it straight down sends undefined into a required prop.

console.log('value passed:', handleSave)

2Fix

Render the child only when the value exists, or give the prop a default through an ES6 default parameter.

function Editor({ title = 'Untitled', onSave }) {
  return <h1 onClick={onSave}>{title}</h1>;
}

3Verify

No warning on the first render.

npm run dev

Confirmed by react.dev

Did this fix it?

Cause 3 of 3

You moved to React 19 and the warnings disappeared

1Check

React 19 removes propTypes checks from the React package, and they are silently ignored. A missing prop is no longer reported.

npm ls react

2Fix

Declare the props in TypeScript instead, so the compiler reports the missing prop. A codemod can convert existing propTypes.

interface EditorProps {
  onSave: () => void;
}
function Editor({ onSave }: EditorProps) { /* ... */ }

3Verify

tsc reports an error where a parent leaves out onSave.

npx tsc --noEmit

Confirmed by react.dev

Did this fix it?

How do I stop it coming back?

  • Type props in TypeScript; propTypes checks are gone in React 19.
  • Use default parameters for optional props instead of defaultProps.
  • Treat data from queries and routes as possibly undefined before it reaches a component.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.