I am passing a message when I redirect to login component which is then passed to Loginform as prop
Login.js
class Login extends React.Component {
constructor(props) { super(props);}
render() {
let msg = "";
console.log(typeof this.props.location.state); // Always exists
console.log(typeof this.props.location.state.redirectmsg); // results in typeError
if(typeof this.props.location.state.redirectmsg!=='undefined') { // results in TypeError
msg=this.props.location.state.redirectmsg;
}
return <Loginform msg={msg} />;
}
}
Redirect
const PrivateRoute = ({ component: Component, ...rest }) => (
<Route {...rest} render={props => (false === true ? <Component {...props} /> : <Redirect to={{ pathname: "/login", state: { redirectmsg: "Kindly login first"} }} />)} />);
But when I try to check if its undefined or not it gives me error.Even console.log gives me error.
Answer : Since state key itself is undefined at first
let msg = ((this.props.location || {}).state || {}).redirectmsg || "";
return <Loginform msg={msg} />;