4.0.0 β’ Published 8 months ago
@putout/plugin-react-router v4.0.0
@putout/plugin-react-router 
πPutout plugin adds ability to migrate to latest version of react router. Not bundled.
Install
npm i putout @putout/plugin-react-router -DRules
Config
{
"rules": {
"react-router/v6-convert-switch-to-routers": "on",
"react-router/v6-convert-component-to-element": "on",
"react-router/v7-split-multi-segment-route": "on"
},
"plugins": ["react-router"]
}convert-switch-to-routes
ReactRouter v6 uses Routers instead of Switch. Check out in πPutout Editor.
β Example of incorrect code
const {Route, Switch} = require('react-router');
const routes = () => (
<Switch>
<Route exact path="/login" component={Login}/>
<Route exact path="/join" component={Join}/>
</Switch>
);β Example of correct code
const {Route, Routes} = require('react-router');
const routes = () => (
<Routes>
<Route exact path="/login" component={Login}/>
<Route exact path="/join" component={Join}/>
</Routes>
);convert-component-to-element
ReactRouter v6 uses element instead of component. Check out in πPutout Editor.
β Example of incorrect code
<Route path="/" component={Home}/>;β Example of correct code
<Route path="/" element={<Home/>}/>;v7-split-multi-segment-route
Split any multi-segment splat
<Route>into a parent route with the path and a child route with the splat.(c) reactrouter.com
Checkout in πPutout Editor:
β Example of incorrect code
<Routes>
<Route path="/" element={<Home/>}/>
<Route path="dashboard/*" element={<Dashboard/>}/>
</Routes>;
createBrowserRouter([{
path: '/',
element: <Home/>,
}, {
path: 'dashboard/*',
element: <Dashboard/>,
}]);β Example of correct code
<Routes>
<Route path="/" element={<Home/>}/>
<Route path="dashboard">
<Route path="*" element={<Dashboard/>}/>
</Route>
</Routes>;
createBrowserRouter([{
path: '/',
element: <Home/>,
}, {
path: 'dashboard',
children: [{
path: '*',
element: <Dashboard/>,
}],
}]);License
MIT