Здравствуйте, это мой первый раз здесь.
Я пытался решить эту проблему в течение последних нескольких часов, и я не могу понять проблему (я новичок, чтобы реагировать).
В PrivateRoute
Я пытаюсь визуализировать компонент аутентификации на основе сеанса.
- сеанс EXISTS => Компонент аутентификации должен отображать
- НЕ СУЩЕСТВУЕТ => полученный компонент долженrender
Я получаю ошибку:
Objects are not valid as a React child (found: [object Promise]).
If you meant to render a collection of children, use an array instead.
помощь будет оценена по достоинству!
Это то, что я сделал до сих пор:
import Login from './Login'
import Register from './Register'
import React, {Component} from 'react'
import Authentication from './Authentication'
import { BrowserRouter , Redirect, Route, Switch,} from 'react-router-dom'
const Authorization= ()=>{
return new Promise ((resolve,reject)=>{
fetch('http://localhost:3000/content',{credentials:'include'}) //includes cookie from different port
.then((res)=>res.json())
.then((data)=>{
if (data.user=="notLogged"){
reject(false)
}else
resolve(true)
})
})
}
const PrivateRoute= async ({ component: Component})=> {
var auth= await Authorization()
console.log(auth);
if (auth){
return <Authentication/>
}else{
return <Component/>
}
}
class Index extends Component{
render(){
return(
<BrowserRouter>
{/* router may have only one child element- switch is the more common */}
<Switch>
<PrivateRoute exact path="/" component={Login}/>
<PrivateRoute exact path="/login" component={Login} />
<Route exact path="/register" component={Register} />
</Switch>
</BrowserRouter>
)
}
}
export default Index