gpt4 book ai didi

javascript - 将自定义数据传递给 React 中的 PrivateRoute 组件

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:40:00 25 4
gpt4 key购买 nike

我是 ReactJS 的新手,并开始使用 React 开发我的第一个应用程序。我一直在观看视频并浏览各种教程,并最终成功地使用 Login 构建了我的第一个 ReactRedux 应用程序。

我用过 AuthWorkflow ReactTraining 网站示例。他们在那里使用了 PrivateRoute 组件来保护 protected 路由。我已经实现了它并开始工作。

问题:
现在我无法将任何自定义数据或类似用户数据发送到 protected 路由。我该如何发送?

代码

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;

如何才能成功地将用户对象发送到 protected 组件?

最佳答案

您需要将传递给 PrivateRoute...rest 参数传递给组件,但并非所有参数都需要对组件使用react。您可以解构 React 路由器所需的其他参数

const PrivateRoute = ({ component: Component, exact, strict, path, ...rest }) => (
<Route
exact={exact}
strict={strict}
path={path}
render={props =>
fakeAuth.isAuthenticated ? (
<Component {...props} {...rest} />
) : (
<Redirect
to={{
pathname: "/login",
state: { from: props.location }
}}
/>
)
}
/>
);

关于javascript - 将自定义数据传递给 React 中的 PrivateRoute 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49044052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com