- Hands-On Design Patterns with React Native
- Mateusz Grzesiukiewicz
- 228字
- 2021-08-13 15:13:00
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.
Check out https://facebook.github.io/react-native/docs/keyboardavoidingview.html for more information.
推薦閱讀
- 潮流:UI設計必修課
- 零基礎學Scratch少兒編程:小學課本中的Scratch創意編程
- 深入淺出Windows API程序設計:編程基礎篇
- Learning SQLite for iOS
- Instant QlikView 11 Application Development
- Monitoring Elasticsearch
- Visual C++數字圖像處理技術詳解
- Spring Boot進階:原理、實戰與面試題分析
- 學習正則表達式
- PHP+MySQL+Dreamweaver動態網站開發從入門到精通(第3版)
- 區塊鏈架構之美:從比特幣、以太坊、超級賬本看區塊鏈架構設計
- 計算機應用基礎(第二版)
- Java從入門到精通(視頻實戰版)
- Isomorphic Go
- Python編程基礎與數據分析