How would you redirect when something isn't present in the DB?

I’m wondering how to do this the “redwood” way:

A user enters the app on a Today page, but if there is no “Today” ready for that given user I want to redirect them to a page where they can create the “Today”.

I’m currently loading a “TodayCell” on the “TodayPage”, should I chuck the navigate in the empty block of that cell? Or would it maybe be better to actually just perform the GraphQL call on the TodayPage itself?

I suppose that when there is no “Today”, your TodayCell will render export const Empty = () => <div>Empty</div>. If so, you can simply replace <div>Empty</div> by <Redirect to={routes.createTodayPage()} />

Don’t forget to import { Redirect, routes } from '@redwoodjs/router' at the top of your cell.

3 Likes

EDIT : @simoncrypta beat me to it ^^
Maybe render a <Redirect> in your empty case

1 Like

:see_no_evil: I came across a couple of time, but somehow my brain had tricked me to think it was just to be used in the Router file…

<Redirect> is definitly a better solution then what I had:

export const Empty = () => {
  React.useEffect(() => {
    navigate(routes.newDay())
  })
  return null
}
1 Like

Did you see the <Redirect> implementation?

const Redirect = ({ to }: RedirectProps) => {
  useEffect(() => navigate(to), [to])
  return null
}

:rofl:

:see_no_evil: Well at least my solution wasn’t as weird as I thought it was haha

I know this is an old thread but I came across this when getting the error “Empty(…): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.”. Thought I’d add a comment in case others have the same issue.

I was trying to use navigate() within the empty component but it didn’t like that I wasn’t rendering anything. Navigating then returning an empty fragment worked a bit better but I still got a console warning (“Cannot update during an existing state transition (such as within render). Render methods should be a pure function of props and state.”).

I’d forgotten about so this was a great help - thanks!