I have the case of a form with input fields. One of those fields must be cleared when I type (keyup) the Enter key. I know I can handle this field as a controlled field (meaning listening on keyup and maintain a copy of the field), or alternatively use two-way binding, but in my use case I can't do the latter, and I'd rather not do the former. My question is thus, how can I force rendering of a Svelte Component?
Typically, the form is displayed with <Component field="value" />, the user modifies the field and clicks the Enter key, and I would like app.set(field, value) to reset the field to value, even and when, to Svelte's eyes, the field property has not changed. Is that possible?
A turn around I tried unsucessfully consists in updating the property inside the Svelte component, with the hope that when app.set(field, value), Svelte will see two different values for the field property and update the component. But that did not seem to work:
<script>
const watchForEnter = ev => {
if (ev.keyCode === 13) {
ev.preventDefault();
const formData = new FormData(ev.target.closest("form"));
const tag = formData.get("tag");
dispatch({ [ADDED_TAG]: tag });
}
};
const updateCurrentTag = ev => {
currentTag = new FormData(ev.target.closest("form")).get("tag");
console.log(`currentTag!`, currentTag)
}
</script>
<form>
<fieldset class="form-group">
<input
name="tag"
class="form-control"
type="text"
placeholder="Enter tags"
on:input={updateCurrentTag}
value={currentTag}
on:keyup={watchForEnter} />
</fieldset>
</form>