1 回答

TA貢獻1853條經(jīng)驗 獲得超6個贊
未捕獲的錯誤:對象作為 React 子對象無效(找到:[object Promise])。如果您打算渲染一組子項,請改用數(shù)組
您收到此錯誤是因為您將異步函數(shù)傳遞給render
,它是一個Promise
對象。您需要傳遞一個 React 組件。這意味著此檢查CT.checkType(type) === false
不能是異步調(diào)用。
所以你試圖做的事情是不可能的。
如果你想根據(jù)請求渲染一個組件,你需要的地方是什么。
您需要聲明檢查以下內(nèi)容:
檢查它是否正在加載
檢查它是否有效
所以你首先設(shè)置路由正在加載(渲染一些加載組件)。承諾完成后,您設(shè)置正在加載為 false 并設(shè)置它是否有效,它將返回Redirect
or Component
。
我為這種情況做了一個非常簡單的要點,但邏輯是這樣的:
const PrivateRoute = ({ component: Component, ...otherProps }) => {
const { isAuthenticated, isLoading } = useContext(AuthContext)
return (
<Route
{...otherProps}
render={props => (
!isLoading
?
(
isAuthenticated
?
<Component {...props} />
:
<Redirect to={otherProps.redirectTo ? otherProps.redirectTo : '/signin'} />
)
:
<Loading />
)}
/>
)
}
在AuthContext它內(nèi)部調(diào)用一個方法,該方法設(shè)置isAuthenticated并isLoading基于異步請求。
添加回答
舉報