TextInput

Loading concept...

๐Ÿ“ TextInput: Your Appโ€™s Magic Notepad

The Story Beginsโ€ฆ

Imagine you have a magic notepad that can listen to everything you write. When you write your name, it remembers it. When you erase something, it notices. When youโ€™re done writing, it can do something special with your words!

Thatโ€™s exactly what TextInput is in React Native โ€” a magic notepad for your app!


๐ŸŽฏ What is TextInput?

TextInput is like a blank box where users can type things. Just like:

  • A search bar where you type what youโ€™re looking for
  • A login form where you enter your username
  • A message box where you write to your friends

Simple Example:

<TextInput
  placeholder="Type here..."
/>

Thatโ€™s it! You just created a typing box! ๐ŸŽ‰


๐Ÿงฑ TextInput Component - The Basics

Your First TextInput

Think of TextInput like an empty notebook page. You place it, and people can write on it!

import { TextInput } from 'react-native';

function MyApp() {
  return (
    <TextInput
      placeholder="Write something..."
    />
  );
}

Making It Look Nice

Just like decorating your notebook, you can style your TextInput:

<TextInput
  style={{
    borderWidth: 1,
    borderColor: 'gray',
    padding: 10,
    borderRadius: 8
  }}
  placeholder="Pretty text box!"
/>
graph TD A[TextInput Component] --> B[User Types] B --> C[Text Appears] C --> D[App Can Use Text]

โš™๏ธ TextInput Configuration - Setting Up Your Notepad

Configuration means setting rules for your notepad. Like telling it:

  • โ€œOnly accept numbers!โ€
  • โ€œHide what I type!โ€ (for passwords)
  • โ€œStart with a capital letter!โ€

๐Ÿ”ข Numbers Only - keyboardType

When you want only numbers (like a phone number):

<TextInput
  keyboardType="numeric"
  placeholder="Enter your age"
/>

Other keyboard types:

Type What It Does
default Normal letters
numeric Numbers only
email-address Has @ symbol
phone-pad Phone numbers

๐Ÿ”’ Secret Mode - secureTextEntry

For passwords โ€” makes dots instead of letters:

<TextInput
  secureTextEntry={true}
  placeholder="Enter password"
/>

What user types: hello123 What they see: โ€ขโ€ขโ€ขโ€ขโ€ขโ€ขโ€ขโ€ข

๐Ÿ“ One Line or Many? - multiline

One line (like a name field):

<TextInput
  placeholder="Your name"
/>

Many lines (like a message):

<TextInput
  multiline={true}
  numberOfLines={4}
  placeholder="Write your story..."
/>

โœ๏ธ Auto-Fix Features

<TextInput
  autoCapitalize="words"
  autoCorrect={true}
  placeholder="Type a sentence"
/>
Setting What Happens
autoCapitalize="none" all lowercase
autoCapitalize="words" Each Word Starts Big
autoCapitalize="sentences" First word big
autoCorrect={true} Fixes typos

๐Ÿ“ Placeholder & Default Value

<TextInput
  placeholder="Hint text (gray)"
  defaultValue="Already filled in!"
/>
  • placeholder: Gray hint that disappears
  • defaultValue: Real text already there
graph TD A[TextInput Config] --> B[keyboardType] A --> C[secureTextEntry] A --> D[multiline] A --> E[autoCapitalize] B --> F[numeric/email/phone] C --> G[Hide password] D --> H[Multiple lines] E --> I[Auto capitals]

๐ŸŽญ TextInput Events - When Things Happen!

Events are like magic signals your notepad sends when something happens:

  • โ€œHey! Someone started typing!โ€
  • โ€œHey! They changed a letter!โ€
  • โ€œHey! They finished typing!โ€

๐Ÿ“ onChangeText - Every Letter Counts!

This fires every time text changes. Itโ€™s the most important event!

import { useState } from 'react';

function MyApp() {
  const [text, setText] = useState('');

  return (
    <TextInput
      value={text}
      onChangeText={(newText) => setText(newText)}
      placeholder="Type here..."
    />
  );
}

What happens:

  1. User types โ€œHโ€ โ†’ setText('H')
  2. User types โ€œHiโ€ โ†’ setText('Hi')
  3. Every keystroke updates!

๐ŸŽฏ onFocus - โ€œIโ€™m Ready to Type!โ€

Fires when user taps the text box:

<TextInput
  onFocus={() => {
    console.log('User clicked the box!');
  }}
  placeholder="Tap me!"
/>

๐Ÿ‘‹ onBlur - โ€œIโ€™m Done Here!โ€

Fires when user taps away from the text box:

<TextInput
  onBlur={() => {
    console.log('User left the box!');
  }}
  placeholder="Tap somewhere else..."
/>

โœ… onSubmitEditing - โ€œEnter Key Pressed!โ€

Fires when user presses Enter/Done/Submit:

<TextInput
  onSubmitEditing={() => {
    console.log('User pressed submit!');
  }}
  placeholder="Press Enter when done"
/>

๐Ÿ”„ onChange - The Full Story

Like onChangeText but gives more info:

<TextInput
  onChange={(event) => {
    console.log(event.nativeEvent.text);
  }}
/>

๐Ÿ“Š All Events Together

function CompleteExample() {
  const [text, setText] = useState('');

  return (
    <TextInput
      value={text}
      onChangeText={setText}
      onFocus={() => console.log('Started!')}
      onBlur={() => console.log('Finished!')}
      onSubmitEditing={() => console.log('Submitted!')}
    />
  );
}
graph TD A[User Action] --> B{What happened?} B --> C[Tapped box] B --> D[Typed letter] B --> E[Tapped away] B --> F[Pressed Enter] C --> G[onFocus fires] D --> H[onChangeText fires] E --> I[onBlur fires] F --> J[onSubmitEditing fires]

๐ŸŒŸ Putting It All Together

Hereโ€™s a complete login form using everything we learned:

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

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

  return (
    <View>
      <TextInput
        value={username}
        onChangeText={setUsername}
        placeholder="Username"
        autoCapitalize="none"
        autoCorrect={false}
      />

      <TextInput
        value={password}
        onChangeText={setPassword}
        placeholder="Password"
        secureTextEntry={true}
      />

      <Text>Hello, {username}!</Text>
    </View>
  );
}

๐ŸŽ‰ Quick Summary

Concept What It Does Example
TextInput Typing box <TextInput />
keyboardType Keyboard style numeric, email-address
secureTextEntry Hide text For passwords
multiline Many lines For long text
onChangeText Text changed Most common event
onFocus Box tapped Started typing
onBlur Left box Finished typing
onSubmitEditing Enter pressed Form submit

๐Ÿš€ You Did It!

Now you know how to:

  • โœ… Create a TextInput (your magic notepad!)
  • โœ… Configure it (set the rules!)
  • โœ… Listen to events (know when things happen!)

TextInput is the bridge between your user and your app. Every message, every search, every login โ€” it all starts with TextInput!

Go build something amazing! ๐ŸŒŸ

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.