官术网_书友最值得收藏!

The TextInput component

We will use this component often in the next section. The general idea is to be able to pass data from a smartphone keyboard. TextInput is used in login and registration forms and many other places where the user needs to send text data to an application.

Let's extend the HelloWorld example from Chapter 1, React Component Patterns, to accept a name:

// Chapter 2 / Example 6 / src / TextInputExample.js
export default class TextInputExample extends React.Component {
state = {
name: null
};

render = () => (
<View style={styles.container}>
{this.state.name && (
<Text style={styles.text}>
Hello {this.state.name}
</Text>
)}
<Text>Hands-On Design Patterns with React Native</Text>
<Text>Chapter 2: View Patterns</Text>
<Text style={styles.text}>
Enter your name below and see what happens.
</Text>
<TextInput
style={styles.input}

onChangeText={name => this.setState({name})}
/>
</View>
);
}
// ... styles skipped for clarity in a book, check source files.

If a user enters text in the TextInput component, then we display the entered text in a short greeting. Conditional rendering uses state to check whether the name has been defined or not. As the user types, the onChangeText event handler is invoked, and the function we passed updates the state with the new name.

Sometimes, native keyboards may overlap with your View component and hide important information. Please get familiar with the  KeyboardAvoidingView component if this is the case in your app.
Check out  https://facebook.github.io/react-native/docs/keyboardavoidingview.html for more information.
主站蜘蛛池模板: 南京市| 永安市| 大同市| 观塘区| 酒泉市| 宾阳县| 桃江县| 永康市| 陇西县| 桐乡市| 甘德县| 营山县| 怀宁县| 轮台县| 平原县| 天水市| 新蔡县| 乳源| 苍梧县| 旬阳县| 西乌珠穆沁旗| 四子王旗| 河南省| 德昌县| 万安县| 万源市| 桃江县| 新兴县| 泸定县| 兰州市| 灌南县| 巢湖市| 沧州市| 黑龙江省| 东丰县| 巴彦县| 江华| 华蓥市| 怀宁县| 平乡县| 中超|