TextInput renders extra characters after `value` was resetted to null
See original GitHub issue- React-native 0.23
- Tested on iOS 9.3 simulator.
- I’m on Mac OS X.
Sorry, I can’t reproduce it on rnplay.
I’m using redux to manage my store.
The initial state of the TextInput is set to null
, that Reset
button would reset the value to null
by sending actions to redux store.
Then things got wired. As the gif below shows:
Some extra characters are left there.
If I set the initial state of TextInput to ''
, and reset it to ''
too, then everything works as expected.
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
React ref null after re-rendering element - Stack Overflow
On the first render, the ref is null and not undefined because of component mounting or unmounting and setting the state. Your ref...
Read more ><input>: The Input (Form Input) element - HTML
A button that resets the contents of the form to default values. Not recommended. A single-line text field for entering search strings.
Read more >FAQs | React Hook Form - Simple React forms validation
How to reset the form? · HTMLFormElement.reset(). This method does the same thing as clicking a form's reset button, and only clears input/select/checkbox...
Read more >dcc.Input - Dash for Python Documentation - Dash Plotly
If there's no selection, this value indicates the offset to the character following the current text input cursor position (that is, the position...
Read more >Select | Quasar Framework
In case you are looking for a dropdown “button” instead of “input” use Button ... Appends clearable icon when a value (not undefined...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think that’s the commit that introduces this behaviour -> https://github.com/facebook/react-native/commit/62177dbb3b4d4bdedf0c7a6fe9140ce049743fbf, more specifically, https://github.com/facebook/react-native/commit/62177dbb3b4d4bdedf0c7a6fe9140ce049743fbf#diff-b9fc78351b7b976a36356ef6f08c1f79R259
This explains why, after entering
ABCD
and resetting value tonull
- the final argument passed tosetText:
is notnull
, but rather the first non-null value (which in this case is letter “A”). Then, that value is stored as long as the component itself is mounted, which also explains why the OP is gettingThe
no matter what is the value right before clickingReset
.Maybe it’s a good moment to deprecate falsy values and just make
value
beReact.PropTypes.string.isRequired
? (more or less a follow up to https://github.com/facebook/react/issues/5013 - bonus points for keeping it consistent). I generally agree with the reasoning described there about null being more a developer error (e.g. mistake when connecting components all together). That would require AFAIK all typeof checks to be replaced withlength > 0
Otherwise, we can just make sure that _getText() is never null, but an empty string in worst case.
Hint - if you can’t / don’t have time to upgrade and are experiencing this - just add
defaultValue={''}
to your<TextInput />
if you haven’t specified it yet.CC: @nicklockwood
Closing this issue because it has been inactive for a while. If you think it should still be opened let us know why.