I have in my app a path that takes 2 type of requests, for the GET
requests I want it to return a React JSX Element/Component
, for the POST
request I want it to run I process then redirect to another page.
My question is, can a Route Handler
return a JSX Component
? and if not, do you have a recommendation to achieve my goal?
It’s not at all recommended to return a <Component/>
from a next route handler. You should only stick to returning data from the route handler that can be used in the component in the client side component. So you can call your route handler in the client component and populate data there.
Now, you might want to conditionally render components based on a particular value from response, that should also be handled at client side only. See https://nextjs.org/docs/app/building-your-application/routing/route-handlers for route handlers.