Blazor WebAssembly: make ChangeEventArgs.Value work both ways for the scope of the event handler #38520
Labels
area-blazor
Includes: Blazor, Razor Components
feature-rendering
Features dealing with how blazor renders components
Milestone
Is your feature request related to a problem? Please describe.
I'd like to be able to undo invalid user input without changing the state of the component or using the
bind:value
Blazor data binding feature.I'm trying to do that inside
oninput
event handler like below (online repl).I've found that
ChangeEventArgs.Value
, while being a get/set property, only works one way, from JS to DotNet. More so,StateHasChanged
doesn't trigger rerendering in this case. Thus, I had to resort to JS interop to revert the changes inside the<input>
box A.(To be clear, I realize this is a horrible hack; it could possible be improved by using
ElementReference
and an isolated JS import, but it all wouldn't be necessary if we could just doe.Value = count
below).Describe the solution you'd like
Make
ChangeEventArgs.Value
work both ways for the scope of the event handler. This would allow for custom input validation logic outside standardbind-xxx
Blazor data binding attributes.For example, I can do this in Svelte (which I find conceptually very close to Blazor):
And here it is in React:
Additional context
https://stackoverflow.com/q/70016908/1768303
To clarify, I simply want to undo whatever I consider an invalid input, retrospectively after it has happened (by handling the change event), without mutating the component's state itself (
counter
here).That is, without Blazor-specific two-way data binding, HTML native
type=number
or pattern matching attributes. I use the number format requirement here as an example; I want to be able to undo any arbitrary input like that.I might be missing something, but I find this surprisingly difficult to do in Blazor, compared to other frameworks. I'd like to be able to use
StateHasChanged
to simply re-render the component in its current state.The text was updated successfully, but these errors were encountered: