导读:本期聚焦于小伙伴创作的《React Native如何实现鸿蒙跨平台简易记事本APP开发》,敬请观看详情,探索知识的价值。以下视频、文章将为您系统阐述其核心内容与价值。如果您觉得《React Native如何实现鸿蒙跨平台简易记事本APP开发》有用,将其分享出去将是对创作者最好的鼓励。

React Native作为成熟的跨平台开发框架,现在也支持适配鸿蒙系统,很多开发者想用它快速开发鸿蒙端应用,下面就以简易记事本APP为例,讲解完整的开发流程。

React Native如何实现鸿蒙跨平台简易记事本APP开发

开发环境准备

首先需要配置好对应的开发环境,确保React Native项目可以正常编译到鸿蒙端:

  • 安装Node.js 16及以上版本,配置好npm或yarn包管理工具
  • 安装React Native CLI,执行命令npm install -g react-native-cli
  • 安装鸿蒙开发工具DevEco Studio,配置好鸿蒙SDK
  • 安装Java Development Kit 11及以上版本,配置好JAVA_HOME环境变量

项目初始化与鸿蒙适配

先初始化一个React Native项目,再添加鸿蒙平台支持:

# 初始化React Native项目
npx react-native init NoteApp
# 进入项目目录
cd NoteApp
# 添加鸿蒙平台支持(需要提前安装好鸿蒙适配插件)
npx react-native-harmonyos-init

核心功能实现

1. 记事本数据模型设计

记事本每条记录需要包含id、标题、内容、创建时间、更新时间几个字段,我们可以用如下接口定义:

// 定义记事本条目类型
interface NoteItem {
  id: string; // 唯一标识
  title: string; // 笔记标题
  content: string; // 笔记内容
  createTime: number; // 创建时间戳
  updateTime: number; // 更新时间戳
}

2. 本地存储功能实现

简易记事本不需要后端服务,用本地存储保存数据即可,这里使用@react-native-async-storage/async-storage库:

import AsyncStorage from '@react-native-async-storage/async-storage';

// 存储所有笔记的key
const NOTES_STORAGE_KEY = 'NOTE_APP_NOTES';

// 获取所有笔记
export const getAllNotes = async () => {
  try {
    const notesStr = await AsyncStorage.getItem(NOTES_STORAGE_KEY);
    return notesStr ? JSON.parse(notesStr) : [];
  } catch (e) {
    console.error('获取笔记失败', e);
    return [];
  }
};

// 保存所有笔记
export const saveAllNotes = async (notes) => {
  try {
    await AsyncStorage.setItem(NOTES_STORAGE_KEY, JSON.stringify(notes));
  } catch (e) {
    console.error('保存笔记失败', e);
  }
};

// 新增笔记
export const addNote = async (title, content) => {
  const allNotes = await getAllNotes();
  const newNote = {
    id: Date.now().toString(),
    title,
    content,
    createTime: Date.now(),
    updateTime: Date.now()
  };
  allNotes.unshift(newNote);
  await saveAllNotes(allNotes);
  return newNote;
};

// 删除笔记
export const deleteNote = async (id) => {
  let allNotes = await getAllNotes();
  allNotes = allNotes.filter(item => item.id !== id);
  await saveAllNotes(allNotes);
};

// 更新笔记
export const updateNote = async (id, title, content) => {
  let allNotes = await getAllNotes();
  const index = allNotes.findIndex(item => item.id === id);
  if (index !== -1) {
    allNotes[index].title = title;
    allNotes[index].content = content;
    allNotes[index].updateTime = Date.now();
    await saveAllNotes(allNotes);
  }
};

3. 页面布局与交互实现

记事本主要有两个页面:列表页和编辑页,下面是实现列表页的核心代码:

import React, { useState, useEffect } from 'react';
import { View, Text, FlatList, TouchableOpacity, StyleSheet } from 'react-native';
import { getAllNotes, deleteNote } from './storage';

const NoteListPage = ({ navigation }) => {
  const [notes, setNotes] = useState([]);

  // 每次页面聚焦时刷新笔记列表
  useEffect(() => {
    const unsubscribe = navigation.addListener('focus', () => {
      loadNotes();
    });
    return unsubscribe;
  }, [navigation]);

  const loadNotes = async () => {
    const allNotes = await getAllNotes();
    setNotes(allNotes);
  };

  const handleDelete = async (id) => {
    await deleteNote(id);
    loadNotes();
  };

  const renderItem = ({ item }) => (
    <TouchableOpacity
      style={styles.noteItem}
      onPress={() => navigation.navigate('EditNote', { noteId: item.id })}
    >
      <View style={styles.noteInfo}>
        <Text style={styles.noteTitle} numberOfLines={1}>{item.title || '无标题'}</Text>
        <Text style={styles.noteContent} numberOfLines={2}>{item.content || '暂无内容'}</Text>
        <Text style={styles.noteTime}>
          更新时间:{new Date(item.updateTime).toLocaleString()}
        </Text>
      </View>
      <TouchableOpacity
        style={styles.deleteBtn}
        onPress={() => handleDelete(item.id)}
      >
        <Text style={styles.deleteText}>删除</Text>
      </TouchableOpacity>
    </TouchableOpacity>
  );

  return (
    <View style={styles.container}>
      <FlatList
        data={notes}
        renderItem={renderItem}
        keyExtractor={item => item.id}
        contentContainerStyle={styles.listContent}
        ListEmptyComponent={
          <Text style={styles.emptyText}>暂无笔记,点击右上角添加</Text>
        }
      />
      <TouchableOpacity
        style={styles.addBtn}
        onPress={() => navigation.navigate('EditNote', { noteId: null })}
      >
        <Text style={styles.addText}>+</Text>
      </TouchableOpacity>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#f5f5f5'
  },
  listContent: {
    padding: 16
  },
  noteItem: {
    flexDirection: 'row',
    alignItems: 'center',
    backgroundColor: '#fff',
    borderRadius: 8,
    padding: 16,
    marginBottom: 12,
    elevation: 2
  },
  noteInfo: {
    flex: 1
  },
  noteTitle: {
    fontSize: 16,
    fontWeight: 'bold',
    marginBottom: 4
  },
  noteContent: {
    fontSize: 14,
    color: '#666',
    marginBottom: 4
  },
  noteTime: {
    fontSize: 12,
    color: '#999'
  },
  deleteBtn: {
    padding: 8,
    backgroundColor: '#ff4d4f',
    borderRadius: 4,
    marginLeft: 12
  },
  deleteText: {
    color: '#fff',
    fontSize: 14
  },
  addBtn: {
    position: 'absolute',
    right: 24,
    bottom: 24,
    width: 56,
    height: 56,
    borderRadius: 28,
    backgroundColor: '#1890ff',
    justifyContent: 'center',
    alignItems: 'center',
    elevation: 4
  },
  addText: {
    color: '#fff',
    fontSize: 24,
    fontWeight: 'bold'
  },
  emptyText: {
    textAlign: 'center',
    color: '#999',
    marginTop: 40,
    fontSize: 14
  }
});

export default NoteListPage;

编译运行到鸿蒙设备

完成代码开发后,执行以下命令编译运行到鸿蒙设备:

# 启动Metro服务
npx react-native start
# 新开终端,运行到鸿蒙设备
npx react-native run-harmonyos

确保鸿蒙设备已经开启开发者模式并连接到电脑,等待编译完成后就可以在设备上看到运行的简易记事本APP,支持新增、编辑、删除笔记,数据会保存在本地不会丢失。

React_Native鸿蒙跨平台开发记事本APP修改时间:2026-05-31 06:04:28

免责声明:​ 已尽一切努力确保本网站所含信息的准确性。网站内容多为原创整理与精心编撰,观点力求客观中立。本站旨在免费分享,内容仅供个人学习、研究或参考使用。若引用了第三方作品,版权归原作者所有。如内容涉及您的权益,请联系我们处理。
内容垂直聚焦
专注技术核心技术栏目,确保每篇文章深度聚焦于实用技能。从代码技巧到架构设计,为用户提供无干扰的纯技术知识沉淀,精准满足专业提升需求。
知识结构清晰
覆盖从开发到部署的全链路。AI、前端、编程、数据库、服务器、建站、系统层层递进,构建清晰学习路径,帮助用户系统化掌握开发与运维所需的核心技术。
深度技术解析
拒绝泛泛而谈,深入技术细节与实践难点。无论是数据库优化还是服务器配置,均结合真实场景与代码示例进行剖析,致力于提供可直接应用于工作的解决方案。
专业领域覆盖
精准对应开发生命周期。从前端界面到后端编程,从数据库操作到服务器运维,形成完整闭环,一站式满足全栈工程师和运维人员的技术需求。
即学即用高效
内容强调实操性,步骤清晰、代码完整。用户可根据教程直接复现和应用于自身项目,显著缩短从学习到实践的距离,快速解决开发中的具体问题。
持续更新保障
专注既定技术方向进行长期、稳定的内容输出。确保各栏目技术文章持续更新迭代,紧跟主流技术发展趋势,为用户提供经久不衰的学习价值。