Summary: Retry for https://github.com/facebook/react-native/pull/14830. There was some problem with the import and hramos instructed to open a new PR with the same changes. See https://github.com/facebook/react-native/pull/14830#issuecomment-315146320 for more details. ----- The tutorial pages tend to have a "follow to the next topic" section at the end of the text. HandlingTextInput.md has handling-touches in the "next" in metadata, but the "follow to the next topic" section linked to ScrollView tutorial page. Thus, the "follow to the next topic" section in the text is updated also to point to handling-touches. Closes https://github.com/facebook/react-native/pull/15176 Differential Revision: D5480608 Pulled By: hramos fbshipit-source-id: 5161d1acad6a3f0401fd5d15d5ff29a0701a1211
2.0 KiB
id | title | layout | category | permalink | next | previous |
---|---|---|---|---|---|---|
handling-text-input | Handling Text Input | docs | The Basics | docs/handling-text-input.html | handling-touches | flexbox |
TextInput
is a basic component that allows the user to enter text. It has an onChangeText
prop that takes
a function to be called every time the text changed, and an onSubmitEditing
prop that takes a function to be called when the text is submitted.
For example, let's say that as the user types, you're translating their words into a different language. In this new language, every single word is written the same way: 🍕. So the sentence "Hello there Bob" would be translated as "🍕🍕🍕".
import React, { Component } from 'react';
import { AppRegistry, Text, TextInput, View } from 'react-native';
export default class PizzaTranslator extends Component {
constructor(props) {
super(props);
this.state = {text: ''};
}
render() {
return (
<View style={{padding: 10}}>
<TextInput
style={{height: 40}}
placeholder="Type here to translate!"
onChangeText={(text) => this.setState({text})}
/>
<Text style={{padding: 10, fontSize: 42}}>
{this.state.text.split(' ').map((word) => word && '🍕').join(' ')}
</Text>
</View>
);
}
}
// skip this line if using Create React Native App
AppRegistry.registerComponent('AwesomeProject', () => PizzaTranslator);
In this example, we store text
in the state, because it changes over time.
There are a lot more things you might want to do with a text input. For example, you could validate the text inside while the user types. For more detailed examples, see the React docs on controlled components, or the reference docs for TextInput.
Text input is one of the ways the user interacts with the app. Next, let's look at another type of input and learn how to handle touches.