I use React Router v4 and this works well.
<Switch>
<Router path="/users/:uid" component={Profile} />
<Router path="*" component={NotFound} />
</Switch>
Okay, if I access the domain /blabla
NotFound component renders well.
But I wonder how can I render NotFound to /users/:uid
when user is not exist.
app.get('/users/:uid', (req, res) {
if( // user exist) {
res.json({user})
}
else res.status(404).json({ message: 'User not exist'})
})
Perhaps I need to hook up server response, but I don't know what is best way.
class Profile extends Component {
componentWillMount = () => {
this.props.fetchUser(uid)
}
render(){
if(!this.props.loading && !this.props.user) return <NotFound />
else return (...)
}
}
Is this correct way? I'm using redux together.