phoenix_live_view icon indicating copy to clipboard operation
phoenix_live_view copied to clipboard

Mounted hook won't rerun when the ID attribute of a form element changed.

Open wongyouth opened this issue 5 years ago • 4 comments

Environment

  • Elixir version (elixir -v): 1.11.3
  • Phoenix version (mix deps): 1.5.7
  • Phoenix LiveView version (mix deps): 0.15.3
  • NodeJS version (node -v): v14.3.0
  • NPM version (npm -v): 6.14.5
  • Operating system: macOS 10.15.7
  • Browsers you attempted to reproduce this bug on (the more the merrier): Chrome Version 87.0.4280.88 (Official Build) (x86_64)
  • Does the problem persist after removing "assets/node_modules" and trying again? Yes/no: Yes

Actual behavior

Here is the simple demo code:

hooks JS code:

const hooks = {
  Mount: {
    mounted() { console.log('mounted') }
  }
}

Live code:

  @impl true
  def mount(_params, _session, socket) do
    {:ok, assign(socket, count: 0)}
  end

  @impl true
  def render(assigns) do
    ~L"""
      <form phx-hook="Mount" id="form-<%= @count %>">
        <input type="text" name="id" value="<%= @count %>">
        <button>Save</button>
      </form>

      <button phx-click='reset'>Reset</button>
    """
  end

  @impl true
  def handle_event("reset", _params, socket) do
    {:noreply, assign(socket, count: socket.assigns.count + 1)}
  end

The mounted function in hooks only executed once even though the ID of the form changed each time when the Reset button clicked.

Expected behavior

The mounted function is expected to be executed each time when the Reset button is clicked.

I figured out that the issue is related with the name of the input field. If I change the input name to anything other than id, the mounted function will be executed as expected.

wongyouth avatar Jan 06 '21 07:01 wongyouth

This is expected behavior, as TIL this about HTML form elements:

Named inputs are added to their owner form instance as properties, and can overwrite native properties if they share the same name (e.g. a form with an input named action will have its action property return that input instead of the form's action HTML attribute).

https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement

So the fix is to not use id as the name as you overrwrite the id property of the form, per the spec. This is not really LiveView related. For example, add your HTML to a regular non-LV page, then run this in the web console:

      <form id="form">
        <input type="text" name="id" />
        <input type="text" name="foobarbaz" />
        <button>Save</button>
      </form>


> let form = document.querySelector("form")
> form.id
> <input type="text" name="id">

> form.foobarbaz
> <input type="text" name="foobarbaz">

Thanks!

chrismccord avatar Jan 07 '21 17:01 chrismccord

Hi @chrismccord Thanks for you explanation. I want to know why LiveView don't use getAttribute method to get the right value?

      <form id="form">
        <input type="text" name="id" />
        <input type="text" name="foobarbaz" />
        <button>Save</button>
      </form>


> let form = document.querySelector("form")
> form.getAttribute('id')
> "form"

wongyouth avatar Jan 08 '21 02:01 wongyouth

morphdom by default will use the ID to key nodes, but we could override it with getAttribute. I'm not convinced that is the way to go tho as we'd have to essentially special case the id reference everywhere we want an ID. This is more of an HTML quirk and this first time I've encountered it doing webdev for many years, so I'd prefer to wait.

chrismccord avatar Jan 08 '21 04:01 chrismccord

Good to know. Thanks.

wongyouth avatar Jan 08 '21 06:01 wongyouth