-
Notifications
You must be signed in to change notification settings - Fork 47.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't update defaultChecked/defaultValue. #4618
Comments
You can specify a dynamic You could also break out of React and use plain javascript to manually reset the input's state (since you're using an uncontrolled input anyway, the component is effectively expecting triggers outside of React to modify it, so this doesn't break the component's mental model/contract). Or you could just "use a controlled input" as you mentioned before, which is the correct solution IMHO. Anyway, there are several options at your disposal. Your job as an engineer is to choose the one that's best for your use case. |
@jimfb I understand that there are some hacks to achieve the desired result (which I am using) although I would expect that the "defaultValue" in my virtual dom accurately reflects the "defaultValue" in the real dom. It just seems odd for react to completely disregard any modifications to defaultValue. |
React pushes you to top-down data flow for a reason. When you have siblings randomly influencing a node's state, it becomes very difficult to reason about the state of your application, and even harder to understand the ramifications of a given change. If you need to be able to influence a component's state, then that state is no longer internal to the component (by definition). Modeling it as internal state is a violation of the abstraction because touching a component's internal state is a violation of the abstraction. If you need to control the component's state, you should probably be using a controlled component, IMO. |
@jimfb I understand your view, and what I want is a "fire and forget" form, which react seems to support. It just seems odd that if uncontrolled forms are supported at all that you don't allow the browser to handle defaultValue and defaultChecked. All I am trying to do is reset an uncontrolled form and it seems impossible in react without hacks or rewriting it into a controlled form (Which I do not necessarily have control over since some of my dependencies use uncontrolled inputs and will not work with the native forms reset). Even if my form was controlled because of this limitation it is unreliable to use the native "reset()" on any form because I cannot trust that react actually updated the default values for my form as my form is re-rendered. So are you saying that react users should never rely on "reset" for forms that may have had their "initialValues" changed? |
@DylanPiercey I'm saying that resetting the form currently "resets" the form elements to their initial values, which is the currently expected behavior (conceptually, the component is saving the initial property into state on first mount, and using that state variable from that point forward; you can't go back in time, so you can't change that initial value, because it was already sent). If you want more control over the state of your You could always implement your own component which behaves like an uncontrolled input (does anything internally) and implements whatever semantics you'd like. |
@DylanPiercey You can also grab the native DOM node and set |
@spicyj My current solution is to create my own inputs and do this: Input = React.createClass({
componentWillReceiveProps(nextProps) {
if (
nextProps.defaultValue !== this.props.defaultValue ||
nextProps.defaultChecked !== this.props.defaultChecked
) {
var input = React.findDOMNode(this);
input.defaultValue = nextProps.defaultValue;
input.defaultChecked = nextProps.defaultChecked;
}
},
render() { return <input {...this.props}/> }
}) Basically it just keeps the defaultValue and defaultChecked in sync with the dom. Which only actually updates the input if it has never been touched by the user, or the form has reset. I guess this is just a little misleading because that is what the browser does by default when you modify "defaultValue". I would propose something like this (as it seems like what we want and is unambiguous): <input initialValue={ 1 } defaultValue={ 2 }/> This could throw with both a defaultValue and initialValue, or initialValue could override defaultValue but only for the initial render. |
Maybe we can make |
@spicyj any further thoughts on this? |
I'm happy to take a pull request that passes defaultValue through to the DOM. It looks like that is linked directly to the |
@DylanPiercey I could not agree with you more |
👍 I definitely endorse this, as I'm also facing the same issue. I simply want the ability to keep using my input as an uncontrolled input but with the ability to change the defaultValue/defaultChecked and reflect that change in the active DOM element. |
@DylanPiercey I just found a good solution for my case, so it might solve yours as well. |
(Marking as good first bug – instructions are in my earlier comment; feel free to ask for clarification.) |
@leandro I mentioned my solution earlier which was simple a wrapper for all of my form inputs with a @spicyj thanks for looking into this. |
👍 |
* Have `defaultValue` reach DOM node for html input box for #4618 * Cleanup and bug fixes for merge.
…6406) * Have `defaultValue` reach DOM node for html input box for facebook#4618 * Cleanup and bug fixes for merge. (cherry picked from commit 4338c8d)
When I rerender a component with "defaultChecked" or "defaultValue" react fails to update the "value" and "checked" attribute accordingly.
Before you say "use a controlled input" I would argue that this is a bug as it basically disallows uncontrolled forms with "reset" buttons. (It will always reset to whatever the initial default value was).
The text was updated successfully, but these errors were encountered: