I am using react router v4 with thunk for routing in my application.
I want to prevent rendering <AccountPage /> component to user who not logged in. I sending fetch request on server with id and token to check in database do user has this token. If it has - render <AccountPage />, if not - redirect home.
I don't understand what is good way to implement the "conditional routing", and i found something which seems almost perfectly fit to my task.
https://gist.github.com/kud/6b722de9238496663031dbacd0412e9d
But the problem is that condition in <RouterIf /> is always undefined, because of fetch's asyncronosly. My attempts to deal with this asyncronously ended with nothing or errors:
Objects are not valid as a React child (found: [object Promise]) ...
or
RouteIf(...): Nothing was returned from render. ...
Here is the code:
//RootComponent
<BrowserRouter>
    <Switch>
        <Route exact path='/' component={HomePage}/>
        <Route path='/terms' component={TermsAndConditionsPage}/>
        <Route path='/transaction(\d{13}?)' component={TransactionPage}/>
        <RouteIf
            condition={( () => {
                if( store.getState().userReducer.id, store.getState().userReducer.token) {
                    // Here i sending id and token on server 
                    // to check in database do user with this id
                    // has this token
                    fetch(CHECK_TOKEN_API_URL, {
                        method: 'post',
                        headers: {'Accept': 'application/json', 'Content-Type': 'application/json'},
                        body: JSON.stringify({
                            id: store.getState().userReducer.id,
                            token: store.getState().userReducer.token
                        })
                    })
                    .then res => {
                        // If true – <RouteIf /> will render <AccountPage />, 
                        // else - <Redirect to="/"> 
                        // But <RouteIf /> mounts without await of this return 
                        // You can see RouteIf file below
                        if(res.ok) return true
                        else return false
                    })
                }
            })()}
            privateRoute={true}
            path="/account"
            component={AccountPage}
        />
    </Switch>
</BrowserRouter>
//RouteIf.js
const RouteIf = ({ condition, privateRoute, path, component }) => {
    // The problem is that condition is 
    // always undefined, because of fetch's asyncronosly
    // How to make it wait untill
    // <RouteIf condition={...} /> return result?
    return condition 
    ? (<PrivateRoute path={path} component={component} />)
    :(<Redirect to="/" />)
}
export default RouteIf
How to make condition wait until fetch return answer? Or maybe there is another, better way to check if user logged in?