Next.js Discord

Discord Forum

fetch user in Suspense and update state of parent without fetching user again

Answered
Dutch posted this in #help-forum
Open in Discord
DutchOP
so I have

export default function Parent = async () => {
  const [state, setState] = useState(0);
  return (
    <>
      <Stepper state={state}></Stepper>
      <Suspense fallback = {<Loading />}>
      <BelowStepper currentState = {state} onClick={setState}></BelowStepper>
      </Suspense>
    </>


Inside BelowStepper, I do some checks like fetching the user from supabase to see if they're logged in, and if they arent, then it the stepper gets set to state step 1, if they are it gets set to step 2.

Does anyone have a better idea how to do this without reloading the user/doing all the checks each time the state changes (and thus BelowStepper gets rerendered) by chance?

It's tricky because I need the fetching of the user to be in a suspense, and want to render the stepper already at the top before it has finished fetching. but I also dont want the fetching of the user and checking wether the step is the correct step to happen again each time the step gets updated
Answered by Dutch
I solved it by splitting BelowStepper into different components for each step, and each of those components checks wether the step is needed or not, and if it isn't, updates the state of the parent to go to the next step, which then causes the parent to render the next step's component instead.
I'll leave this here for now incase anyone's curious or has a better solution
View full answer

1 Reply

DutchOP
I solved it by splitting BelowStepper into different components for each step, and each of those components checks wether the step is needed or not, and if it isn't, updates the state of the parent to go to the next step, which then causes the parent to render the next step's component instead.
I'll leave this here for now incase anyone's curious or has a better solution
Answer