Newbie here. I have problems with the following code. I want to define a protected "Dashboard" page, as follows, but is giving me this binding error:
Binding element 'Component' implicitly has an 'any' type.ts(7031)
import * as React from "react";
import { Route, Router, Redirect } from "react-router-dom";
import Dashboard from "../features/dashboard";
import LoginContainer from "../features/login/containers/LoginContainer";
import SignUpContainer from "../features/signup/containers/SignUpContainer";
import { createBrowserHistory } from "history";
const history = createBrowserHistory();
export default () => (
<Router history={history}>
<Route path="/" component={LoginContainer} />
<Route path="/login" component={LoginContainer} />
<Route path="/register" component={SignUpContainer} />
<PrivateRoute path="/dashboard" component={Dashboard} />
</Router>
);
function PrivateRoute({ component: Component, ...rest }) {
return (
<Route
{...rest}
render={props =>
localStorage.getItem("MyStoredUser") ? (
<Component {...props} />
) : (
<Redirect
to={{
pathname: "/login"
}}
/>
)
}
/>
);
}
Basically I am using the same code as it is here:Protected routes and authentication with React Router v4 and eact Router - Redirects
What am I doing wrong?
You havn't defined any types, and you apparently have typescript configured to forbid implicit any in arguments. You'll need to add in types to satisfy the typescript compiler:
import * as React from 'react';
import {Route, Router, Redirect, RouteProps} from 'react-router';
const PrivateRoute: React.FC<RouteProps> = ({ component: Component, ...rest }) => {
return (
// ... etc
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With