Thuta Learning
React Native
IntermediateMobile Developmentအလယ်အလတ်

Handling User Input

React Nativeသင်ခန်းစာ 920 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Handling User Input ရဲ့ React နဲ့ native platform behavior ကိုရှင်းပြနိုင်ရန်
  • Accessibility, failure နဲ့ lifecycle cases စမ်းနိုင်ရန်
  • Maintainable, testable နဲ့ performant React Native code ရေးနိုင်ရန်

ရိုးရိုးလေး ရှင်းပြမယ်

Handling User Input ကို ရိုးရိုးလေးနားလည်မယ်

TextInput က text ရိုက်ထည့်ဖို့ component ပါ (HTML ရဲ့ <input> နဲ့ တူ) — value prop နဲ့ onChangeText callback ကို useState နဲ့ ပေါင်းသုံးရင် controlled input ဖြစ်ပါတယ် (user ရိုက်တိုင်း state update)။ Button က basic built-in component ပေမယ့် styling limited ပါတယ် — custom design လိုအပ်ရင် TouchableOpacity/Pressable ကို View+Text နဲ့ ပေါင်းသုံးလေ့ ရှိပါတယ်။

Problem → Why → Solution

ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ

onChangeText ကို onChange (web-style) ထင်ပြီး wrong prop name သုံးခြင်း — React Native က onChangeText ဖြစ်ရမည်

ဖြစ်လေ့ရှိတဲ့အမှား

TextInput ကို value/onChangeText မတွဲဘဲ uncontrolled အဖြစ်ထားခြင်း — state နဲ့ sync မဖြစ်

Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်

Handling User Input မှာ navigation state, form state, server state နဲ့ device state တို့၏ owner ကိုခွဲပါ။ Rapid taps, back gesture, deep link, process restart နဲ့ keyboard-only/screen-reader flow များကိုပါစမ်းပါ။

Mental Model
  1. JavaScript state
  2. Native component + layout
  3. Android / iOS UI

Handling User Input ရဲ့ React နဲ့ native platform behavior ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

javascript
import { useState } from 'react';
import { View, TextInput, TouchableOpacity, Text, StyleSheet } from 'react-native';

export default function LoginForm() {
  const [username, setUsername] = useState('');
  const [password, setPassword] = useState('');

  return (
    <View style={styles.form}>
      <TextInput
        style={styles.input}
        placeholder="Username"
        value={username}
        onChangeText={setUsername}
      />
      <TextInput
        style={styles.input}
        placeholder="Password"
        secureTextEntry
        value={password}
        onChangeText={setPassword}
      />
      <TouchableOpacity style={styles.button} onPress={() => console.log(username)}>
        <Text style={styles.buttonText}>Login</Text>
      </TouchableOpacity>
    </View>
  );
}

const styles = StyleSheet.create({
  form: { padding: 16 },
  input: { borderWidth: 1, borderColor: '#ccc', borderRadius: 8, padding: 12, marginBottom: 12 },
  button: { backgroundColor: '#3b82f6', padding: 14, borderRadius: 8, alignItems: 'center' },
  buttonText: { color: 'white', fontWeight: '600' },
});

Handling User Input မှာ navigation state, form state, server state နဲ့ device state တို့၏ owner ကိုခွဲပါ။ Rapid taps, back gesture, deep link, process restart နဲ့ keyboard-only/screen-reader flow များကိုပါစမ်းပါ။

Code ကို အဆင့်လိုက်ဖတ်မယ်

  1. JavaScript stateimport { useState } from 'react';

    TextInput က text ရိုက်ထည့်ဖို့ component ပါ (HTML ရဲ့ <input> နဲ့ တူ) — value prop နဲ့ onChangeText callback ကို useState နဲ့ ပေါင်းသုံးရင် controlled input ဖြစ်ပါတယ် (user ရိုက်တိုင်း state update)။ Button က basic built-in component ပေမယ့် styling limited ပါတယ် — custom design လိုအပ်ရင် TouchableOpacity/Pressable ကို View+Text နဲ့ ပေါင်းသုံးလေ့ ရှိပါတယ်။

  2. Native component + layoutsecureTextEntry

    Handling User Input မှာ navigation state, form state, server state နဲ့ device state တို့၏ owner ကိုခွဲပါ။ Rapid taps, back gesture, deep link, process restart နဲ့ keyboard-only/screen-reader flow များကိုပါစမ်းပါ။

  3. Android / iOS UI});

    Handling User Input example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။

Execution Flow
  1. JavaScript state
  2. Native component + layout
  3. Android / iOS UI

Success

Handling User Input ရဲ့ React နဲ့ native platform behavior ကိုရှင်းပြနိုင်ရန်

Failure

onChangeText ကို onChange (web-style) ထင်ပြီး wrong prop name သုံးခြင်း — React Native က onChangeText ဖြစ်ရမည်

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

TextInput က text ရိုက်ထည့်ဖို့ component ပါ (HTML ရဲ့ <input> နဲ့ တူ) — value prop နဲ့ onChangeText callback ကို useState နဲ့ ပေါင်းသုံးရင် controlled input ဖြစ်ပါတယ် (user ရိုက်တိုင်း state update)။ Button က basic built-in component ပေမယ့် styling limited ပါတယ် — custom design လိုအပ်ရင် TouchableOpacity/Pressable ကို View+Text နဲ့ ပေါင်းသုံးလေ့ ရှိပါတယ်။

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

Login form တစ်ခု ဆောက်မယ်ဆိုရင် username/password အတွက် TextInput နှစ်ခု, submit button တစ်ခု လိုအပ်ပါတယ် — password field ကို secureTextEntry={true} ထည့်ရင် dot (••••) အဖြစ် ဖျောက်ပြပါတယ်။ TouchableOpacity ကို Button အစား သုံးရင် custom color/border/padding ပေးလို့ရပါတယ် — Button component ကတော့ platform default style ကိုပဲ သုံးရမှာမို့ customize ခက်ပါတယ်။

Code နမူနာ

javascript
import { useState } from 'react';
import { View, TextInput, TouchableOpacity, Text, StyleSheet } from 'react-native';

export default function LoginForm() {
  const [username, setUsername] = useState('');
  const [password, setPassword] = useState('');

  return (
    <View style={styles.form}>
      <TextInput
        style={styles.input}
        placeholder="Username"
        value={username}
        onChangeText={setUsername}
      />
      <TextInput
        style={styles.input}
        placeholder="Password"
        secureTextEntry
        value={password}
        onChangeText={setPassword}
      />
      <TouchableOpacity style={styles.button} onPress={() => console.log(username)}>
        <Text style={styles.buttonText}>Login</Text>
      </TouchableOpacity>
    </View>
  );
}

const styles = StyleSheet.create({
  form: { padding: 16 },
  input: { borderWidth: 1, borderColor: '#ccc', borderRadius: 8, padding: 12, marginBottom: 12 },
  button: { backgroundColor: '#3b82f6', padding: 14, borderRadius: 8, alignItems: 'center' },
  buttonText: { color: 'white', fontWeight: '600' },
});
You should see
Login form ပေါ်မှာ username/password ရိုက်ထည့်နိုင်ပြီး Login button press လိုက်ရင် username ကို console မှာ print ထုတ်လာမည်။

၅ မိနစ် စမ်းကြည့်

Login form ကို ကိုယ်တိုင် ဆောက်ကြည့်ပါ — TextInput ၂ ခု (username, password) နဲ့ TouchableOpacity button တစ်ခု ပါစေပါ။

သတိလေးတစ်ချက်

Password TextInput မှာ secureTextEntry ပေးရုံနဲ့ password ကို 'လုံခြုံ' ပြီလို့ မယူဆပါနှင့် — visual masking ပဲ ဖြစ်ပြီး, actual transmission/storage security ကို backend side မှာ သီးခြား handle ရပါမယ်။

Real-world usage

လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ

Handling User Input example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။

Mini challenge

ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ

Handling User Input example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
javascript
import { useState } from 'react';
import { View, TextInput, TouchableOpacity, Text, StyleSheet } from 'react-native';

export default function LoginForm() {
  const [username, setUsername] = useState('');
  const [password, setPassword] = useState('');

  return (
    <View style={styles.form}>
      <TextInput
        style={styles.input}
        placeholder="Username"
        value={username}
        onChangeText={setUsername}
      />
      <TextInput
        style={styles.input}
        placeholder="Password"
        secureTextEntry
        value={password}
        onChangeText={setPassword}
      />
      <TouchableOpacity style={styles.button} onPress={() => console.log(username)}>
        <Text style={styles.buttonText}>Login</Text>
      </TouchableOpacity>
    </View>
  );
}

const styles = StyleSheet.create({
  form: { padding: 16 },
  input: { borderWidth: 1, borderColor: '#ccc', borderRadius: 8, padding: 12, marginBottom: 12 },
  button: { backgroundColor: '#3b82f6', padding: 14, borderRadius: 8, alignItems: 'center' },
  buttonText: { color: 'white', fontWeight: '600' },
});

ရလဒ်: Handling User Input ရဲ့ React နဲ့ native platform behavior ကိုရှင်းပြနိုင်ရန်

အရိပ်အမြွက် ကြည့်ရန်

Handling User Input example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။

Quick Quiz 1 / 3

Handling User Input ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

ဘယ်အမှားကို အရင်ဆုံးရှောင်သင့်လဲ။

Quick Quiz 3 / 3

Real project မှာ ဘယ်လိုအသုံးချသင့်လဲ။

ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ

  • JavaScript state → Native component + layout → Android / iOS UI
  • Handling User Input မှာ navigation state, form state, server state နဲ့ device state တို့၏ owner ကိုခွဲပါ။ Rapid taps, back gesture, deep link, process restart နဲ့ keyboard-only/screen-reader flow များကိုပါစမ်းပါ။
  • onChangeText ကို onChange (web-style) ထင်ပြီး wrong prop name သုံးခြင်း — React Native က onChangeText ဖြစ်ရမည်
  • Handling User Input example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။

React Native Documentation — React Native

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • onChangeText ကို onChange (web-style) ထင်ပြီး wrong prop name သုံးခြင်း — React Native က onChangeText ဖြစ်ရမည်
  • TextInput ကို value/onChangeText မတွဲဘဲ uncontrolled အဖြစ်ထားခြင်း — state နဲ့ sync မဖြစ်
  • Simulator တစ်ခု၏ happy path မှန်ရုံဖြင့် real devices, release builds, permissions, accessibility, offline နဲ့ lifecycle paths အားလုံးမှန်ပြီဟုယူဆခြင်း။

လက်တွေ့လေ့ကျင့်ခန်း

Handling User Input example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: Login form ပေါ်မှာ username/password ရိုက်ထည့်နိုင်ပြီး Login button press လိုက်ရင် username ကို console မှာ print ထုတ်လာမည်။

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Handling User Input | Thuta Learning