Keyboard dismisses while typing TextInput in nested functional component React Native

前端 未结 1 1953
醉梦人生
醉梦人生 2021-01-19 05:04

I have this strange issue, keyboard keeps closing while typing when TextInput is placed inside Child Functional Component. This issue does not exist if TextInput is placed d

相关标签:
1条回答
  • 2021-01-19 05:40

    your SignInForm function (which is treated like React component, because its capitalized and called as JSX) is declared inside your SignInScreenC component. This means that every render, new type of React component is created.

    1. SignInScreenC renders first time: creates SignInForm component, instantiates it and renders it
    2. SignInScreenC renders second time: creates another, completely different SignInForm component, instantiates it again, effectively unmounting old SignInForm and rendering new SignInForm in it's place
    3. since old input is unmounted, you lose keyboard focus

    This is due to the way React handles rendering: whenever it encounters different type of element that should be rendered in place of an old element, old one will be unmounted. To react, every new SignInForm is different from the previous one as you keep constantly creating new functions

    Solutions:

    1. create separate SignInForm component outside of SignInScreenC and pass all the necessary data as props
    2. or, instead of const SignInForm = () => return (...) use const renderSignInForm = () => return (...), and while rendering, instead of <SignInForm/> call it like {renderSignInForm()}. This way it will not be treated like a component and will not be a subject to unmounts
    0 讨论(0)
提交回复
热议问题