Bug(regression): PropTypes.resetWarningCache has no effect in react@next
See original GitHub issueReact version: 0.0.0-235a6c4af
Steps To Reproduce
- render component with exptected propType warnings
- see logged errors
- call
PropTypes.resetWarningCache()
- render component with exptected propType warnings
- no more logged errors
Link to code example:
import React from "react";
import ReactDOM from "react-dom";
import PropTypes from "prop-types";
function Component({ children = "setting default to not crash react" }) {
return children;
}
Component.propTypes = { children: PropTypes.node.isRequired };
const rootElement = document.createElement("div");
ReactDOM.render(<Component />, rootElement);
PropTypes.resetWarningCache();
ReactDOM.render(<Component />, rootElement);
Behavior on 0.0.0-235a6c4af
Behavior on 16.13.0
The current behavior
Warnings are logged once
The expected behavior
Warnings are logged twice. This was the behavior in 16.13.0
Context
Useful for testing custom propTypes warnings. When a test is watched and modules can’t be reset between runs resetWarningCache
was useful. Otherwise the test behavior would change between test runs.
Pretty sure this is caused by inlining prop-types
in #18127. I think we can keep inlining the code and only import the cache so that it is shared with the regular prop-types
package and PropTypes.resetWarningCache()
keeps working.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:28 (21 by maintainers)
Top Results From Across the Web
prop types not working in react application - Stack Overflow
I am trying to use proptypes for in my react-redux app, but it is not working, or I am doing something wrong. this...
Read more >How to validate React props using PropTypes - LogRocket Blog
Learn how to validate props with React PropTypes, React's internal mechanism for adding type checking to component props.
Read more >Don't Call PropTypes Warning - React
This pattern by itself is fine, but triggers a false positive because React thinks you are calling PropTypes directly. The next section explains...
Read more >PropTypes class - react_client.react_interop library - Dart API
PropTypes class. Runtime type checking for React props and similar objects. See: reactjs.org/docs/typechecking-with-proptypes.html See: ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is a bit unfortunate. But to be honest, we didn’t really sign up to respect this setting in React itself. The fact that the
prop-types
module used by React was technically shared is an implementation detail that could easily change, e.g. if we pinned our version.While I probably won’t convince you,
jest.resetModules()
inbeforeEach
+ CommonJS in tests does this too. That’s what we’ve been using. It works pretty well and resets other state you might want to make isolated.The purpose was to get rid of imports.
Prop-types is a legacy feature. It’s not something that is super important to React going forward. We try to keep it working, but it’s not a first-class “React API”. There is no need for us to keep the implementation separate when the contract is so simple and we could just call into the user code from React here.
On the other hand, changes related to ESM and just our bundling process is always more complicated when there’s dependencies involved. We have so few dependencies that this isn’t worth the complexity. It is easier to get rid of them.
See https://github.com/mui-org/material-ui/pull/20451