最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Svelte 3: Event Handling not Working as Expected - Stack Overflow

programmeradmin0浏览0评论

On Svelte 3.12.1, when event handler onFilesChange is triggered, it unexpectedly triggers onFileClick.

<script>
  let files = [];
  function previewImage(file) {
    return "<img src='" + file.previewURL + "' />";
  }
  function onFileClick(file) {
    console.log("onFileClick");
    files.splice(files.findIndex(x => x.name === file.name), 1);
    files = files;
  }
  function onFilesChange(event) {
    console.log("onFilesChange");
    let inputFiles = event.target.files;
    for (var i = 0; i < inputFiles.length; i++)
      files.push({inputFile: inputFiles.item(i), previewURL: URL.createObjectURL(inputFiles.item(i))});
    files = files;
  }
</script>

<input accept="image/*" multiple name="files" type="file" on:change={onFilesChange}/>
{#each files as file}
  <figure on:click={onFileClick(file)}>
    {@html previewImage(file)}
  </figure>
{/each}

But if I slightly change to using arrow functions, then it works as expected. Why is this happening, and what is the proper way to handle DOM events in Svelte 3?

<input accept="image/*" multiple name="files" type="file" on:change={(event) => onFilesChange(event)}/>
{#each files as file}
  <figure on:click={(file) => onFileClick(file)}>
    {@html previewImage(file)}
  </figure>
{/each}

On Svelte 3.12.1, when event handler onFilesChange is triggered, it unexpectedly triggers onFileClick.

<script>
  let files = [];
  function previewImage(file) {
    return "<img src='" + file.previewURL + "' />";
  }
  function onFileClick(file) {
    console.log("onFileClick");
    files.splice(files.findIndex(x => x.name === file.name), 1);
    files = files;
  }
  function onFilesChange(event) {
    console.log("onFilesChange");
    let inputFiles = event.target.files;
    for (var i = 0; i < inputFiles.length; i++)
      files.push({inputFile: inputFiles.item(i), previewURL: URL.createObjectURL(inputFiles.item(i))});
    files = files;
  }
</script>

<input accept="image/*" multiple name="files" type="file" on:change={onFilesChange}/>
{#each files as file}
  <figure on:click={onFileClick(file)}>
    {@html previewImage(file)}
  </figure>
{/each}

But if I slightly change to using arrow functions, then it works as expected. Why is this happening, and what is the proper way to handle DOM events in Svelte 3?

<input accept="image/*" multiple name="files" type="file" on:change={(event) => onFilesChange(event)}/>
{#each files as file}
  <figure on:click={(file) => onFileClick(file)}>
    {@html previewImage(file)}
  </figure>
{/each}
Share Improve this question edited Sep 22, 2019 at 20:11 JuniorSD asked Sep 22, 2019 at 20:02 JuniorSDJuniorSD 491 silver badge5 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

The on:whatever event handler values need to be functions that will be called with the DOM event as an argument. on:change={onFilesChange} is fine because onFilesChange is a function which expects an event as its argument. on:click={onFileClick(file)} is not because that will call onFileClick(file) right away, and the return value from that is expected to be an event handler.

You should use on:click={() => onFileClick(file)} so that the event handler is a function, which will be passed the DOM event (which gets discarded) and then calls onFileClick(file).

发布评论

评论列表(0)

  1. 暂无评论