Skip to content
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

Passing the content of a <TextInput /> on end and submit callbacks. #838

Open
ZakaHaceCosas opened this issue Dec 3, 2024 · 0 comments
Open

Comments

@ZakaHaceCosas
Copy link

Introduction

RN's TextInput has many callbacks, like onChangeText, onSubmitEditing, onEndEditing..., however only onChangeText passes the current text as an argument to the callback function. Would it be possible to do the rest with onEnd and onSubmit?

Details

RN's documentation itself provides an example similar to this one:

const [text, onChangeText] = React.useState('Useless Text');

return (
  <TextInput
    style={styles.input}
    onChangeText={onChangeText}
    // or if we want to get explicit / do something else with the text:
    // onChangeText={(value) => {onChangeText(value)})
    value={text}
  />
)

Here, every keystroke updates the state (text) and triggers a re-render of the component. Wouldn't it be more performant to update the state when the user is finished?

For instance:

const [text, onChangeText] = React.useState('Useless Text');

return (
  <TextInput
    style={styles.input}
    onSubmitEditing={(value) => {onChangeText(value)})
    value={text}
  />
)

This should perform better because you only update the state once, when the user is done with editing. It would also make stuff more flexible, since the dev would be able to interact with the raw value always, without needing a useState() that changes on every edit just to have the input's text content.

Discussion points

  1. Reduce state updates for performance
  2. Make <TextInput /> more flexible
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant