Adding eventListener in an onMount shows error in console

I created a form ref and added an event listener in a component with tsx ´´´ let formRef; . . .
onMount(() => { console.log(formRef); formRef.addEventListener("input", (e) => { if (e.target.name !== "enable") { setIsJustSubmitted(false); } }); }); <form ref={formRef} onSubmit={submit}> . . . ´´´ The form gets printed a lot of times and the error i get is "Uncaught (in promise) TypeError: formRef is undefined" I tried multiple things but none did work, like trying to catch the error, making the event listener be added conditionally when the formRef is filled etc
16 Replies
Alex Lohr
Alex Lohr15mo ago
You can either use the ref function to set the listener manually or use the on:input prop to add a normal event. You can also use oncapture:input for a capturing event.
akerbeltz
akerbeltz15mo ago
how would that change the code?
Alex Lohr
Alex Lohr15mo ago
// Using the ref function
<form ref={node => node.addEventListener(...)}>

// Using on:event
<form on:input={ev => ev.target.name !== "enable" && setIsJustSubmitted(false)}>
// Using the ref function
<form ref={node => node.addEventListener(...)}>

// Using on:event
<form on:input={ev => ev.target.name !== "enable" && setIsJustSubmitted(false)}>
You might need to extend some types if you are using TS.
akerbeltz
akerbeltz14mo ago
Thanks! How is it that with the onMount have this strange behaviour in my code (just to understand this better)? The form has a conditional show in diferent locations, could this cause some problems with the ref? The Show component is mounting again their childs when they are not "shown" and change the visibility? which difference there is between on:input and oninput? (if there is any) in the second option
Alex Lohr
Alex Lohr14mo ago
on:x is the same as onx, but onx is obviously unsupported. It's just an elegant way to add custom events.
akerbeltz
akerbeltz14mo ago
@lexlohr on:input was marking me an error but oninput worked fine with the rest of the code, it's strange in typescript i had to cast the type in ev.target as HTMLFormElement too haha so long the code there's a way i can reuse this oninput callback in other forms ?
Alex Lohr
Alex Lohr14mo ago
You can use on:input if you declare input as FormEvent in CustomEvents: https://www.solidjs.com/docs/latest/api#special-jsx-attributes
SolidJS
Solid is a purely reactive library. It was designed from the ground up with a reactive core. It's influenced by reactive principles developed by previous libraries.
Alex Lohr
Alex Lohr14mo ago
Unfortunately, even that is not too reusable, since it easily clashes with other event handlers; I had the same issue with input-mask. For the sake of simplicity, I decided to expose the event handler instead so the users could bind it themselves.
akerbeltz
akerbeltz14mo ago
what happened with the input-mask?
Alex Lohr
Alex Lohr14mo ago
As I said, I directly exposed the event handlers, so users could add them together with their own, if necessary. Another option would be to use a directive. Which basically gives you the ref to the element.
akerbeltz
akerbeltz14mo ago
I come from React and i'm having trouble with some things. You could make something similar to a higher order component in solidjs? Some things are clearer but the trouble is for the habit of doing things in a certain way haha
Alex Lohr
Alex Lohr14mo ago
Yes and no. The problem is that the Component receives the children as result of DOM expressions, so there is no virtual DOM that could be manipulated.
akerbeltz
akerbeltz14mo ago
I see
Alex Lohr
Alex Lohr14mo ago
You can manipulate the children, but if you want the code to be isomorphic, that's going to be difficult, since those can be HTML Elements in the client and { t: string } on the server. It really depends on your use case. There are almost always idiomatic patterns that are simpler in the long run. It sounds like you want to create a form handling library.
Alex Lohr
Alex Lohr14mo ago
You could create a <Form> component that internally adds an input event using the event-listener primitive https://primitives.solidjs.community/package/event-listener#directive-usage
Solid Primitives
A library of high-quality primitives that extend SolidJS reactivity
akerbeltz
akerbeltz14mo ago
it's a patch of a functionality i need that is missing in one library to handle forms and errors of the forms. The library is solid-form-handler. The problem is that with that library there's no easy way to reset the form (to set the dirty of the form to false) but that mantain the data present on the inputs. So I created the isJustSubmitted signal as a patch to this, but i wonder how to reuse this functionality (there's not too much but if i can reuse it's better I think). I'll look into that
Want results from more Discord servers?
Add your server
More Posts
refetch not changing the signal of the createResourcewhen trying to update the data of the signal of a resource it didn't update. I have the same issue wStuck in 'rendering index.html' in solid-start buildHello, when I run build in my project, it seems to be stuck on rendering index.html. Any possible reHandle union type in storeHi peeps, I would like to store union type like this `type Post = NewPost | DeletedPost;` in a storPassing For Id between InputGroup and Input using childrenI'm trying to pass create a JSX structure like: ``` <InputGroup label="Email" error="that's not Are there any guarantees on the order of createMemo / createComputed execution when siblings?This example suggests there are none: https://playground.solidjs.com/anonymous/32dc8fda-6b7d-40df-8ccreateMemo with `equals: false` not reactive when mutating referenceI am trying to prevent to create `DOMMatrix` with each calculation, so I thought to mutate 1 with `cHow to have an effect that does not subscribe to the signal's values?I have this effect: https://github.com/nikitavoloboev/kuskus/blob/main/src/components/Page.tsx#L15 multiple sitesdoes solid start support multiple sites (on different domains) but with one node instance? is there createResource mutations / page stateHello, For I'm using createResource for my solid-start project for fetching post user can interact Why autofocus on input element not work with solid second timeI have this code: https://github.com/nikitavoloboev/kuskus Here I do conditional render of `NewTodo