Default values issue
Issue:
I need help implementing a form field that:
Initializes with real-time data (unitPrice from Firebase)
Updates automatically only when other property changes (so basiclly defailt value needs to be udpdated when other external property passed
Stops auto-updating after user modification
Resets to new defaults when parent context changes (assetId)
Current Behavior:
When switching assets (assetId changes), price input doesn't update immediately
Real-time price updates sometimes override user modifications
Form initialization race conditions when data loads after mount
9 Replies
quickest-silver•4mo ago
could you share your current attempt at solving it? it's fine if it doesn't work, but it helps to build off of what you've constructed to suggest fixes
inland-turquoiseOP•4mo ago
I currently pass price default value as zero
and then i have useEffect
to avoid confusion initall values here are ment for editing flow
this will make the form-re render with the new. value everytime the unit price changes
i really can't find good solution for this issue
quickest-silver•4mo ago
and the unit price is editable by the user? If not, then it shouldn‘t be a form value. If it can, then you‘ll overwrite the user‘s custom set value which sounds like confusing UX
inland-turquoiseOP•4mo ago
Yes it is editable by the user, And i don't want to override the user value, i just pre fill the form with some inital values and user can change it, but if user changed his asset Id for example it needs to change the data
quickest-silver•4mo ago
then a listener on the assetId field sounds more like what you‘re looking for
inland-turquoiseOP•4mo ago
But assetid is not field in form it is prop to the component
quickest-silver•4mo ago
do you need form meta to be preserved between changing assetId? or is a complete rerender okay?
inland-turquoiseOP•4mo ago
Complete re render okay
quickest-silver•4mo ago
React‘s key property can be used to inform React that a component needs a complete rerender
so a wrapper with assetId around the component containing the form could force rerenders