簡體   English   中英

使用 React Typescript 展開運算符

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM