question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React.memo and React.lazy ignore propTypes

See original GitHub issue

Do you want to request a feature or report a bug? Bug

What is the current behavior? PropTypes.*.isRequired with React.memo doesn’t get triggered in the console when the required prop is undefined.

import React, { memo } from "react";
import ReactDOM from "react-dom";
import PropTypes from "prop-types";

// ********************************************
// It should appear two propTypes console errors
// ********************************************

// without memo(..): it will throw the propTypes error like it should
const MemoButton = memo(() => {
  return <button>Memo Button</button>;
});
MemoButton.propTypes = {
  memocolor: PropTypes.string.isRequired
};

// it will throw the error in the console
const Button = () => {
  return <button>Button</button>;
};
Button.propTypes = {
  color: PropTypes.string.isRequired
};

const rootElement = document.getElementById("root");
ReactDOM.render(
  <>
    <MemoButton />
    <Button />
  </>,
  rootElement
);

Edit 501jppv64n

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

// affected versions
"prop-types": "15.6.2",
"react": "16.6.1",
"react-dom": "16.6.1",

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

10reactions
gaearoncommented, Nov 22, 2018

This turned out to be harder than expected 😅 Fix in https://github.com/facebook/react/pull/14298.

3reactions
Jessidhiacommented, Nov 13, 2018

The update to use react-is is not yet @latest; the @latest release still uses PropTypes.func.

This is the current check on master: https://github.com/ReactTraining/react-router/blob/d5979813abcd53f7ab0e518248374c61c22f8e15/packages/react-router/modules/Route.js#L131-L137

Read more comments on GitHub >

github_iconTop Results From Across the Web

React v16.6.0: lazy, memo and contextType
Class components can bail out from rendering when their input props are the same using PureComponent or shouldComponentUpdate . Now you can do ......
Read more >
How To Avoid Performance Pitfalls in React with memo ...
The memo function will compare props and prevent re-renders if no props change, but in this case the showExplanation prop does change, so...
Read more >
React.lazy and React.useMemo Together | by Alexey Gaynulin
memo don't work together — actually, React. useMemo doesn't work when its parent is lazy loaded via React. Suspense. I'm not sure —...
Read more >
Top 11 React Performance Optimization Techniques in 2023
1. Windowing or List Virtualization in React Applications · 2. Key Coordination for List Rendering · 3. Lazy loading Images in React ·...
Read more >
React.createElement: type is invalid error after bundling and ...
import React from 'react'; import PropTypes from 'prop-types'; ... Symbol.for("react.lazy"):60116,A="function"===typeof ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found