Skip to content

Controlled form input with signal value that needs to respond to defaultValue changes #4443

Answered by developit
rejhgadellaa asked this question in Q&A
Discussion options

You must be logged in to vote

This is a decent approach. I have a variant of this that doesn't allow the argument and signal value to diverge (any render overwrites if different) that I have been calling useLiveSignal:
https://gist.github.com/developit/a72311c247756f24da5b22d19c9dad48#file-signal-utils-js-L46:L55

It's slightly different than yours in that it doesn't need the ref, but I think for your case what you came up with is actually pretty clever.

Re your first question:

Does preact behave the same as react when it comes to changing state during render? (as in: immediately retry the render?)

Yes. If you mutate useState values synchronously during rendering, Preact will immediately retry rendering your function…

Replies: 3 comments 4 replies

Comment options

You must be logged in to vote
1 reply
@rejhgadellaa
Comment options

Comment options

You must be logged in to vote
2 replies
@rschristian
Comment options

@rejhgadellaa
Comment options

Comment options

You must be logged in to vote
1 reply
@rejhgadellaa
Comment options

Answer selected by rejhgadellaa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants