1
Hello!
I have a file containing a single function, it should return a boleano from a condition using the data from the Redux Storage. I can connect the file without being a Reactcomponent to Redux?
The function is responsible for authenticating the application. Therefore:
import { connect } from "react-redux";
const isAuthenticated = ({ user }) => {
console.log("auth.js", user);
return true;
}
const mapStateToProps = (state) => ({
user: state.user,
});
export default connect(mapStateToProps, null)(isAuthenticated);
In the route file I try to call this function isAuthenticated()
:
import React from "react";
import { BrowserRouter, Switch, Route, Redirect } from "react-router-dom";
import { connect } from "react-redux";
import isAuthenticated from "./utils/auth";
const PrivateRoute = ({ component: Component, ...rest }) => {
if (isAuthenticated()) return <Route component={Component} {...rest} />;
else
return (
<Redirect
to={`/login?redirect=${window.location.pathname}`}
{...rest}
/>
);
};
const AnonymousRoute = ({ component: Component, ...rest }) => {
if (!isAuthenticated()) return <Route component={Component} {...rest} />;
else return <Redirect to={`/app`} {...rest} />;
};
return (
<BrowserRouter>
<Switch>
...ROTAS
</Switch>
</BrowserRouter>
);
export default connect(mapStateToProps)(Routes);
However I get the following error:
Ps.: I am beginner in the world React JS. Thank you!
Hello, thanks in advance for the contribution. Referring to the quotation regarding the server check, this is exactly what I would like to do. My token data is persisted in localStorage, and I need to recover it using React-Redux. For this reason I need to make the connection.
– Augusto Henrique