This repository has been archived by the owner on Sep 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 356
React Portals can't be validated with prop-types #167
Labels
Comments
Any update on this? I'm experiencing the same issue. The React type definitions state that ReactPortal is a valid ReactNode, but it seems prop-types disagrees at runtime. React version: 16.3.1 Steps To Reproduce
Code sample:import React, { useState, useRef, useEffect } from "react";
import ReactDOM from "react-dom";
import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";
import logo from "./logo.svg";
import "./App.css";
function App() {
const [mountPoint, setMountPoint] = useState();
const mountRef = useRef(null);
useEffect(() => {
if (mountRef.current) {
setMountPoint(mountRef.current);
}
}, [setMountPoint, mountRef]);
return (
<>
<div ref={mountRef} id="test-id"></div>
<Router>
{mountPoint
? ReactDOM.createPortal(
React.createElement(Link, { to: "/other-page" }, "OTHER PAGE"),
mountPoint
)
: null}
<Switch>
<Route path="/" exact>
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
</Route>
<Route path="/other-page">
<Link to="/">HOME</Link>
<h1>Other page</h1>
</Route>
</Switch>
</Router>
</>
);
}
export default App; Link to code example: The current behaviorThe app renders and behaves as expected but this console error is thrown:
The expected behaviorNo error thrown because |
ljharb
added
bug
help wanted
and removed
new validator request
Requests for a new kind of validator.
labels
Aug 11, 2020
any workaround to suppress this warning? this is quite annoying as this warning is false positive |
Any news on this one please ? |
In my case it helped to simply wrap <div>
<div>bla</div>
{createPortal(<div>Hello, World!</div>, targetEl)}
</div> do <div>
<div>bla</div>
<>
{createPortal(<div>Hello, World!</div>, targetEl)}
</>
</div> Console error is gone, now. |
9 tasks
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
PropTypes.node is not accepting element, created by React.createPortal as valid
Is it possible to treat element created by React.createPortal as iterator over it's children or treat portal itself as valid node?
The text was updated successfully, but these errors were encountered: