0

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.

JuntaeKim
  • 5,364
  • 15
  • 54
  • 97

1 Answers1

0
class Profile extends Component {
   render(){
       if(!this.props.loading && !this.props.user) {
        this.props.history.push('/bababa') // you can jump to notFound
        return
      }
       else return (...)
   }
 }

is this?

kaykiek
  • 26
  • 2