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
1 Answer
Reset to default 6The 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)
.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745182342a4615475.html
评论列表(0条)