[idea] React-enhanced input/textarea/select should be addons #2255
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
EDIT: I recommend clicking this link to view changes as github diff ignored the file moves.
This RFC-PR removes all React-specific enhancements from
ReactDOMInput
,ReactDOMTextarea
andReactDOMSelect
. The controlled (and still optionally uncontrolled) implementations are now found inReact.addons.*
instead and are fully backwards-compatible. The implementations provided by React core are now bare-bones, focusing only on exposing the DOM in a predictable way, much like the rest of ReactDOM.Related: #2242, #2241, #2220, #1968, the list goes on.
Why was it flawed
ReactDOMInput
,ReactDOMTextarea
andReactDOMSelect
are very complex implementations and they're still far from perfect (and quite frequently patched). Worse, the implementations are really rather hacky. A generic neat and robust implementation simply doesn't seem possible.<select>
has to refreshoption.selected
for all options every render, the logic for<input type="radio">
is down-right horrendous and there are a number of edge-cases that do not behave as you would expect.Trying to make
<input>
,<textarea>
and<select>
controlled for the generic use-case is flawed and as such doesn't belong in React core.Why is this better
ReactDOM*
is mostly wrapped by users and packaged as their ownMyTextfield
,MyButton
, etc components and React makes it fantastically simple. So there's no reason why they should be forced to use and bundle sub-optimal implementations even when they have significantly simpler requirements.Implementing the React-enhanced controlled versions of
<input>
,<textarea>
and<select>
as addons/components is trivial. It allows the core implementations to stay lean and focus only on exposing the DOM consistently (and to patchReactDOM*
independently of React's release schedule). It allows the community to tailor the amount of logic to their own needs and to ignore edge-cases that simply aren't relevant, if they don't care, just useReact.addons.*
. Providing "pure" implementations of the controls would be trivial too for even more benefit.There's no reason why this should be in React core. This PR as-is shaves ~4.3kb/1.2kb (~3-4%) of React core, can significantly reduce performance overhead for edge-cases and keeps React ever slightly less needlessly opinionated.
Result
ReactDOM*
now only accept initial values.<input initialValue={...} initialChecked={...} />
,<textarea initialValue={...} />
and<select initialValue={...} />
(initialSelected
?), feel free to object toinitial
vsdefault
.React.addons.*
are backwards-compatible implementations building on-top of theReactDOM*
exposed by React core.Codemod:ing existing codebases should be trivial.
PS. This PR is not thoroughly coded/tested (yet) and includes some unrelated fixes (and obviously breaks a lot of tests), but the idea is sound and the implementation does work. If you agree with the premise I could go ahead and clean it up.
cc @zpao @petehunt @sebmarkbage @jordwalke @spicyj @chenglou