React-router-dom を使っナビゲーションを作っていた際に下記の warning が表示されてしまいました。
React does not recognize the `computedMatch` prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase `computedmatch` instead. If you accidentally passed it from a parent component, remove it from the DOM element.
computedMatch という props は自分では書いてなかったので、🤔となりましたが、どうやら <Switch> で使われているようでした。
警告が表示された コンポーネント
// Navigation.jsx import { NavLink, Route, Switch } from 'react-router-dom'; export default function Navigation() { return ( <nav> <Switch> <ul> <li><NavLink to="/" exact>HOME</NavLink></li> <li><NavLink to="/page" exact>Page</NavLink></li> <Route path="/page"> <li><NavLink to="/page/sub" exact>Sub Page</NavLink></li> </Router> </ul> </Switch> </nav> ); }
<Switch> の直下は <Route> か <Redirect> でなければならない
Renders the first child
<Route>or<Redirect>that matches the location.
cf.- React Router: Declarative Routing for React.js
<Switch> 直下が <ul> になっていたのが原因なので、下記のように修正すれば警告は表示されなくなりました。
// Navigation.jsx import { NavLink, Route, Switch } from 'react-router-dom'; export default function Navigation() { return ( <nav> <ul> <li><NavLink to="/" exact>HOME</NavLink></li> <li><NavLink to="/page" exact>Page</NavLink></li> <Switch> <Route path="/page"> <li><NavLink to="/page/sub" exact>Sub Page</NavLink></li> </Router> </Switch> </ul> </nav> ); }
間にDOM挟むサブナビゲーションとかだと、都度 <Switch> で囲わないとダメそう…
[参考]
Switch…持ってないから島にはいけない…
