SKTFighting
SKTFighting

Reputation: 3

React - nothing is returned

I have problems with my private route (admin). Here's how it looks like:

const PrivateRoute = ({ component: Component, ...rest }) => {
  const { user } = rest.user

  return (
    user.length
    ? (
      <Route
        {...rest}
        render={ props => {
          user[0].tier > 1
          ? (
            <Component {...props} />
          ) : (
          <Redirect
            to={{
              pathname: '/app/unauthorized',
            }}
          />
          )
        }}
      />
      ) : (
      <Redirect
        to={{
        pathname: '/app/unauthorized',
        }}
      />
    )
  )
}

When user is logged and has specific privileges this route should have returned component that is sent as a props like this:

<PrivateRoute path={`${url}/`} component={ AdminDashboard } />

But I got an error that nothing is returned from render function. Am I missing something obiovus?

Upvotes: 0

Views: 81

Answers (2)

Tomasz Mularczyk
Tomasz Mularczyk

Reputation: 36179

You do not return component in a render function:

  <Route
    {...rest}
    render={ props => {
      /// THERES NO RETURN STATEMENT ...
      user[0].tier > 1
      ? (
        <Component {...props} />
      ) : (
      <Redirect
        to={{
          pathname: '/app/unauthorized',
        }}
      />
      )
    }}
  />

Upvotes: 2

sathish kumar
sathish kumar

Reputation: 1507

I think you forget to return render method inside Route follow bellow code

 const PrivateRoute = ({ component: Component, ...rest }) => {
  const { user } = rest.user

  return (
    user.length
    ? (
      <Route
        {...rest}
        render={ props => 
          user[0].tier > 1
          ? (
            <Component {...props} />
          ) : (
          <Redirect
            to={{
              pathname: '/app/unauthorized',
            }}
          />
          )
        }
      />
      ) : (
      <Redirect
        to={{
        pathname: '/app/unauthorized',
        }}
      />
    )
  )
}

Upvotes: 1

Related Questions