question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React Native TextInput Field being cleared when onBlur is called

See original GitHub issue

In my React Native app, if the user blurs a TextInput Field too quickly after typing, for example by hitting the submit key, then the field’s value is being updated to the empty string in both the store and on the screen.

If the user waits a second or two after they finish typing before hitting submit, then this issue does not occur.

Looking into this issue, I can see that the TextInput’s onBlur function is being called with the event.nativeEvent.text value equal to the empty string '' (even though the TextInput had a value before blurring the field), which I presume is being used by the onBlur function passed through by redux-form to update the field’s value in the store.

This suggests the problem lies somewhere in react-native, however I can’t recreate this behaviour reliably using just a TextInput - the problem becomes consistent when I apply redux-form’s Field to the input, so I am raising this issue here for now.

My current workaround is to overwrite the onBlur function like so:

<TextInput 
    {...input} 
    onBlur={() => input.onBlur()}
/>

This allows for validation to still take place but as the event object isn’t being passed, redux-form doesn’t update the field’s value in the store when onBlur occurs.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

10reactions
fbarraillacommented, Jun 26, 2017

Hi, I found a workaround who works well thanks to this answer : https://stackoverflow.com/a/40851395

You can actually pass the input.onBlur function to the onEndEditing prop of the field instead of onBlur.

ConnectedInput = ({ input: { onChange, onBlur, ...restInput } }) => (
  <TextInput
    onChangeText={onChange}
    onEndEditing={onBlur}
    {...restInput}
  />
)
6reactions
pviniscommented, Sep 21, 2017

you can do onBlur={(evt) => input.onBlur(evt.nativeEvent.text)}

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I get the value in TextInput when onBlur is called?
In React Native, you can get the value of the TextInput from e.nativeEvent.text .
Read more >
TextInput
If true , the text field will blur when submitted. The default value is true for single-line fields and false for multiline fields....
Read more >
onblur Event
The onblur event occurs when an element loses focus. The onblur event is often used on input fields. The onblur event is often...
Read more >
Controller | React Hook Form - Simple React forms validation
React Hook Form embraces uncontrolled components and native inputs, ... field, name. string. Input's name being registered. field, ref. React.Ref.
Read more >
TextInput // React Native for Web - GitHub Pages
If true , the text field will blur when submitted. The default value is true for single-line fields and false for multiline fields....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found