[英]Spread operator with React Typescript
我有一个单页应用程序,重定向实现为
const PrivateRoute = ({ component: Component, ...rest }) => (
<Route {...rest} render={(props) => (
Acc.isLoggedIn === true
? <Component {...props} />
: <Redirect to={{
pathname: '/login',
state: { from: props.location }
}} />
)} />
)
export { PrivateRoute };
我正在尝试更新它以进行这样的工作
interface IPrivateRouteProps {
}
interface IPrivateRouteState {
isAuthenticated?: boolean
}
export class PrivateRoute extends React.Component<IPrivateRouteProps, IPrivateRouteState> {
constructor(props: IPrivateRouteProps) {
super(props);
this.state = ({ isAuthenticated: null });
}
componentDidMount() {
Acc.isAuthenticated()
.then(isAuthenticated => {
this.setState({ isAuthenticated: isAuthenticated });
});
}
render() {
if (this.state.isAuthenticated == null) {
return null;
}
else if (this.state.isAuthenticated) {
return <Route {...rest} render={(props) => (<Component {...props} />)}/>
}
else {
return <Route {...rest} render={(props) => (<Redirect to={{ pathname: '/login', state: { from: props.location } }} />)}/>
}
}
}
如何使用打字稿中的传播运算符传递道具?
更新
不用说,我不仅仅依靠客户端代码进行身份验证。
我已经尝试过这个,但它没有帮助: 在打字稿 2.3.1 中在 tsx 中传播反应道具
rest
没有定义。
你的意思是:
return <Route {...this.props} render={(props) => (<Component {...props} />)}/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.