React Native入门:用Expo在手机上录入第一条任务

昨天的任务清单在浏览器里运行。今天把最小的一步搬到手机上:输入"检查链接",按一下"保存",让文字从输入框进入下方的任务卡。先做好这个动作,就能分清页面结构、输入事件和React状态分别负责什么。

1. 建一个只有一页的Expo项目

在安装了受当前Expo SDK支持的Node.js LTS的电脑上运行以下命令。选择blank模板,便于直接编辑根目录的App.js。本文使用JavaScript模板和React Native核心组件;若项目入口是路由目录,文件位置应以所选模板为准。

sh 复制代码
npx create-expo-app@latest PocketTasks --template blank
cd PocketTasks
npx expo start

开发服务器启动后,使用与项目SDK兼容的Expo Go打开终端二维码。手机和电脑需要能互相访问。已有Android模拟器时,先启动模拟器,再在终端按a。页面一直连不上时先确认终端仍在运行,再核对设备网络和Expo Go支持的SDK;终端显示了二维码只代表启动了开发服务器。

2. 从输入框到任务卡,中间有两个值

draft 记录键盘正在编辑的内容,saved记录最近一次确认保存的标题。两者共用一个状态时,每输入一个字符,下方"已保存"的标题就会跟着变。把确认动作单独建模,才能做到输入第二条时,第一条仍留在卡片里。

3. 替换App.js并打开页面

jsx 复制代码
import { useState } from 'react';
import { View, Text, TextInput, Pressable, StyleSheet } from 'react-native';

export default function App() {
  const [draft, setDraft] = useState('');
  const [saved, setSaved] = useState('');
  const title = draft.trim();

  function save() {
    if (!title) return;
    setSaved(title);
    setDraft('');
  }

  return (
    <View style={styles.page}>
      <Text style={styles.heading}>口袋任务</Text>
      <Text>任务标题</Text>
      <TextInput
        accessibilityLabel="任务标题"
        style={styles.input}
        value={draft}
        placeholder="例如:检查链接"
        onChangeText={setDraft}
        returnKeyType="done"
        onSubmitEditing={save}
      />
      <Pressable
        accessibilityRole="button"
        accessibilityState={{ disabled: !title }}
        disabled={!title}
        onPress={save}
        style={[styles.button, !title && styles.disabled]}
      >
        <Text style={styles.buttonText}>保存</Text>
      </Pressable>
      <Text>最近保存:{saved || '暂无任务'}</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  page: { flex: 1, justifyContent: 'center', padding: 24, gap: 12 },
  heading: { fontSize: 26, fontWeight: 'bold' },
  input: { borderWidth: 1, borderColor: '#697583', padding: 12 },
  button: { backgroundColor: '#245a84', alignItems: 'center', padding: 12 },
  disabled: { opacity: 0.45 },
  buttonText: { color: 'white' }
});

View 承载布局,文字放进Text 。TextInput的onChangeText直接给出新字符串,value决定输入框显示什么。Pressable的onPress和键盘提交共用save,两个入口因此采用同一条去空格规则。StyleSheet里的padding使用数值;这里不需要把Web的像素字符串搬过来。

4. 用三步验收输入与保存

  1. 只输入空格,按钮应保持禁用。即使用键盘提交,save也会在更新前返回。
  2. 输入" 检查链接 "并保存。输入框应清空,卡片应显示去掉首尾空格的"检查链接"。
  3. 输入"校对标题"但先不保存。卡片仍应显示"检查链接";按键盘完成后才替换为新标题。

这里只展示最近一条任务。完全退出并重新启动应用后,本地组件状态会重新初始化。后续扩展为长列表时,需要保存多条记录并为每条记录分配稳定编号;当前页面用单个字符串就能表达验收需求。

5. 思维导图

#mermaid-svg-N5eptYXa2BKfyiLa{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-N5eptYXa2BKfyiLa .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-N5eptYXa2BKfyiLa .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-N5eptYXa2BKfyiLa .error-icon{fill:#552222;}#mermaid-svg-N5eptYXa2BKfyiLa .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-N5eptYXa2BKfyiLa .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-N5eptYXa2BKfyiLa .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-N5eptYXa2BKfyiLa .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-N5eptYXa2BKfyiLa .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-N5eptYXa2BKfyiLa .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-N5eptYXa2BKfyiLa .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-N5eptYXa2BKfyiLa .marker{fill:#333333;stroke:#333333;}#mermaid-svg-N5eptYXa2BKfyiLa .marker.cross{stroke:#333333;}#mermaid-svg-N5eptYXa2BKfyiLa svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-N5eptYXa2BKfyiLa p{margin:0;}#mermaid-svg-N5eptYXa2BKfyiLa .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-N5eptYXa2BKfyiLa .cluster-label text{fill:#333;}#mermaid-svg-N5eptYXa2BKfyiLa .cluster-label span{color:#333;}#mermaid-svg-N5eptYXa2BKfyiLa .cluster-label span p{background-color:transparent;}#mermaid-svg-N5eptYXa2BKfyiLa .label text,#mermaid-svg-N5eptYXa2BKfyiLa span{fill:#333;color:#333;}#mermaid-svg-N5eptYXa2BKfyiLa .node rect,#mermaid-svg-N5eptYXa2BKfyiLa .node circle,#mermaid-svg-N5eptYXa2BKfyiLa .node ellipse,#mermaid-svg-N5eptYXa2BKfyiLa .node polygon,#mermaid-svg-N5eptYXa2BKfyiLa .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-N5eptYXa2BKfyiLa .rough-node .label text,#mermaid-svg-N5eptYXa2BKfyiLa .node .label text,#mermaid-svg-N5eptYXa2BKfyiLa .image-shape .label,#mermaid-svg-N5eptYXa2BKfyiLa .icon-shape .label{text-anchor:middle;}#mermaid-svg-N5eptYXa2BKfyiLa .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-N5eptYXa2BKfyiLa .rough-node .label,#mermaid-svg-N5eptYXa2BKfyiLa .node .label,#mermaid-svg-N5eptYXa2BKfyiLa .image-shape .label,#mermaid-svg-N5eptYXa2BKfyiLa .icon-shape .label{text-align:center;}#mermaid-svg-N5eptYXa2BKfyiLa .node.clickable{cursor:pointer;}#mermaid-svg-N5eptYXa2BKfyiLa .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-N5eptYXa2BKfyiLa .arrowheadPath{fill:#333333;}#mermaid-svg-N5eptYXa2BKfyiLa .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-N5eptYXa2BKfyiLa .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-N5eptYXa2BKfyiLa .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-N5eptYXa2BKfyiLa .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-N5eptYXa2BKfyiLa .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-N5eptYXa2BKfyiLa .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-N5eptYXa2BKfyiLa .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-N5eptYXa2BKfyiLa .cluster text{fill:#333;}#mermaid-svg-N5eptYXa2BKfyiLa .cluster span{color:#333;}#mermaid-svg-N5eptYXa2BKfyiLa div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-N5eptYXa2BKfyiLa .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-N5eptYXa2BKfyiLa rect.text{fill:none;stroke-width:0;}#mermaid-svg-N5eptYXa2BKfyiLa .icon-shape,#mermaid-svg-N5eptYXa2BKfyiLa .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-N5eptYXa2BKfyiLa .icon-shape p,#mermaid-svg-N5eptYXa2BKfyiLa .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-N5eptYXa2BKfyiLa .icon-shape .label rect,#mermaid-svg-N5eptYXa2BKfyiLa .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-N5eptYXa2BKfyiLa .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-N5eptYXa2BKfyiLa .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-N5eptYXa2BKfyiLa :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 移动任务页
Expo启动
设备打开页面
TextInput更新草稿
确认与去空格
Text显示保存结果
View与样式组织布局

官方资料:Expo项目模板 · 在设备上运行 · TextInput组件

总结要点

键盘输入改变草稿,确认操作改变已保存标题。把这两层状态分开,就能直接从代码推断输入、取消输入和保存时屏幕应有的变化。

用相同的保存函数连接按钮和键盘,空白校验也只需写一次。真实设备上的第一轮检查应同时覆盖这两个提交入口。

下一篇讲移动布局与长列表,把单条任务卡扩展为可以滚动的任务清单。

👉 如果你觉得这篇文章对你有所帮助,欢迎点赞、收藏、分享!😊

相关推荐
小宋102120 小时前
手机 Agent 读屏别默认整屏上交:按视图节点授权、敏感面遮蔽与采集预算,用泄漏率和任务成功率验收
人工智能·智能手机
code2cat2 天前
React入门之任务列表:交换两行后,展开的详情跟着谁走
前端·javascript·react.js
科技拓维2 天前
海信电视默认桌面怎么改?GenTV桌面手机就能换
智能手机
niyongsheng3 天前
CopyCraft 字帖工坊:React + Electron 做一个全平台本地字帖生成器
react.js·electron·开源
用户298698530143 天前
在 React 中实现 Excel 文本与数字互转的完整实践
javascript·react.js·excel
少年的范儿3 天前
React Native + Expo 实战 04|让笔记界面响应操作:组件与状态
react native·react.js·typescript
陈如水3 天前
AI Agent 主流架构
前端·javascript·react.js
开开心心就好3 天前
电脑禁止自动休眠工具,还能按负载定时
算法·智能手机·pdf·电脑·word·音视频·lua
鲸鹏芯海微电子科技 (上海) 有限公司4 天前
手机直连卫星为什么难在频段、功率和天线?
智能手机·低轨卫星·鲸鹏芯海·知玺