I create an eLearning app using react and want to split routes to many users (pupils, teachers, and admins) so I use this method
NewRoute.js
const NewRoute = ({
component: Component,
type: type,
...rest
}) => (
<Route
{...rest}
render={ async props => {
console.log(await CT.checkType(type));
return CT.checkType(type) === false ? (
<Redirect to='/login' />
) : (
<Component {...props} />
)
}
}
/>
);
checkType.js
export default {
checkType: async (type) => {
try{
const res = await axios.get('/api/check/');
console.log('api response',res);
return true
} catch(err) {
console.error(err.message);
return false
}
}
}
Uncaught Error: Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead.