denoland / fresh

The next-gen web framework.
https://fresh.deno.dev
MIT License
12.54k stars 648 forks source link

Submit form from Island doesn't render in POST handler #2242

Closed hapaxlife closed 10 months ago

hapaxlife commented 10 months ago

The test project in Deploy and github in links below

funny-chicken-85.deno.dev

https://github.com/hapaxlife/Test_Post

// routes/test.tsx
export const handler: Handlers = {
  async GET(_req, ctx) {
    return ctx.render({
      method: "GET",
    });
  },
  async POST(req, ctx) {
      return ctx.render({
        method: "POST",
      });
    }
  }

  //
  export default function ChartPricePage(props: PageProps) {
    console.log(props.data.method);
    // log GET and POST 

    // If POST send from code in route, OK
    // If POST send from code in an island, => don't render
  return (
    <>
    Method : {method} => always GET print if form send from island
    </>
  )
  }
CAYdenberg commented 10 months ago

Your island is submitting using the fetch API, so when the response comes back, you have to update state somewhere to update the DOM. (This is equivalent to what used to be called ajax). In this case you'd want your handler to return JSON or something rather than rendering.

If you remove the handleSubmit event handler, you'll see that the page reloads with the data you passed from your POST route handler.

hapaxlife commented 10 months ago

That make sense for the refresh. Thx