Rendering of flash causes form to to be re-rendered... why?

Hey gang, I’ve got a simple form that I’m using to get some data from a textarea in liveview.

I don’t have a validate form handler so the form data on the server always has the textarea data as an empty string (it was set there in the mount function using to_form).

When I save the form I get the textarea data from the params passed into the handle_event function, then I return {:noreply, socket}.

After the save the textarea on the client keeps its state (i.e. the text I entered doesnt disappear) despite the servers textarea form data being that same empty string set in mount. This checks out as nothing has changed on the server to justify a re-render for the client for that textarea.

The weird part comes when I throw a put_flash into the mix.

If the save handler returns {:noreply, put_flash(socket, :info, “Great success”)} the flash causes the simple forms textarea to also be re-rendered on the client, which sets it to the empty string on the server.

This all boils down to the question “Why does the flash cause the simple forms textarea to be re-rendered given that the server shouldnt see a reason to re-render that component?” I was expecting the flash to be rendered and nothing else to change.

This is expected behaviour. You need to track your form’s state if you want to keep it around. This would happen on any change to assigns, not just flash.

See this thread for discussion about it.

Yep, that thread pretty much answers all of my questions on the form re-rendering! Thank you!

1 Like