我是ReactJS的新手,并使用React开始我的第一个应用程序。我一直在观看视频并浏览各种教程,最后设法通过Login登上我的第一个ReactRedux应用程序。
我使用了ReactTraining网站的AuthWorkflow示例。他们使用PrivateRoute
组件来保护受保护的路由。我已经实施了它并且正在实施它。
问题:
现在,我无法将任何自定义数据或类似用户数据发送到受保护路由。我怎么发送它?
代码
import React from "react";
import {
BrowserRouter as Router,
Route,
Link,
Redirect,
withRouter
} from "react-router-dom";
////////////////////////////////////////////////////////////
// 1. Click the public page
// 2. Click the protected page
// 3. Log in
// 4. Click the back button, note the URL each time
const AuthExample = () => (
<Router>
<div>
<AuthButton />
<ul>
<li>
<Link to="/public">Public Page</Link>
</li>
<li>
<Link to="/protected">Protected Page</Link>
</li>
</ul>
<Route path="/public" component={Public} />
<Route path="/login" component={Login} />
// Here I want to pass the user data to protected component.
<PrivateRoute path="/protected" component={Protected} user={username:'ariful', email:'test@example.com'}/>
</div>
</Router>
);
const fakeAuth = {
isAuthenticated: false,
authenticate(cb) {
this.isAuthenticated = true;
setTimeout(cb, 100); // fake async
},
signout(cb) {
this.isAuthenticated = false;
setTimeout(cb, 100);
}
};
const AuthButton = withRouter(
({ history }) =>
fakeAuth.isAuthenticated ? (
<p>
Welcome!{" "}
<button
onClick={() => {
fakeAuth.signout(() => history.push("/"));
}}
>
Sign out
</button>
</p>
) : (
<p>You are not logged in.</p>
)
);
const PrivateRoute = ({ component: Component, ...rest }) => (
<Route
{...rest}
render={props =>
fakeAuth.isAuthenticated ? (
<Component {...props} />
) : (
<Redirect
to={{
pathname: "/login",
state: { from: props.location }
}}
/>
)
}
/>
);
const Public = () => <h3>Public</h3>;
// show the username here
const Protected = (props) => <h3>Protected Username: {props.user.username}</h3>;
class Login extends React.Component {
state = {
redirectToReferrer: false
};
login = () => {
fakeAuth.authenticate(() => {
this.setState({ redirectToReferrer: true });
});
};
render() {
const { from } = this.props.location.state || { from: { pathname: "/" } };
const { redirectToReferrer } = this.state;
if (redirectToReferrer) {
return <Redirect to={from} />;
}
return (
<div>
<p>You must log in to view the page at {from.pathname}</p>
<button onClick={this.login}>Log in</button>
</div>
);
}
}
export default AuthExample;
如何成功将用户对象发送到受保护组件?
答案 0 :(得分:4)
您需要将传递给...rest
的{{1}}参数传递给组件,但并非所有参数都需要对组件做出反应。您可以构建React路由器
PrivateRoute
答案 1 :(得分:1)
查看问题所在:
<PrivateRoute
path="/protected"
component={Protected}
user={username:'ariful', email:'test@example.com'} />
现在在PrivateRoute
组件中,用户对象将作为道具传递,它将在rest
对象中可用,同时rest
将具有{{1}所需的数据所以要使它更通用。
而不是传递用户对象总是传递Route
,现在休息将拥有Route所需的数据,数据将是传递给组件的数据,写成如下:
data
将该数据传递给const PrivateRoute = ({ component: Component, data, ...rest }) => (
<Route
{...rest}
render={props =>
fakeAuth.isAuthenticated ? (
<Component {...props} {...data} />
) : (
<Redirect
to={{
pathname: "/login",
state: { from: props.location }
}}
/>
)
}
/>
);
答案 2 :(得分:0)
带有react-redux钩子的功能组件的PrivateRoute
import React from 'react';
import { Route, Redirect } from 'react-router-dom';
import {useSelector} from 'react-redux'
const PrivateRoute = ({ component: Component, auth, ...rest }) => {
const isAuthenticated = useSelector((state)=>state.isAuthenticated)
return (
<Route
{...rest}
render={props =>
isAuthenticated === true ? (
<Component {...props} />
) : (
<Redirect to="/login" />
)
}
/>
)
}
export default PrivateRoute;
答案 3 :(得分:0)
没什么花哨的,只需调用render函数并将一些数据传递到组件中即可。
<PrivateRoute
render = {() =>
<Component
data = {value}
/>}
path = "/path"
/>