diff --git a/1224/react-native-base64.md b/1224/react-native-base64.md new file mode 100644 index 0000000000000000000000000000000000000000..b5b6859504860b69ce1e2cebfebb0475ca27c447 --- /dev/null +++ b/1224/react-native-base64.md @@ -0,0 +1,133 @@ +> 模板版本:v0.1.2 + +

+

react-native-base64

+

+

+ + License + +

+ +> [!tip] [Github 地址](https://github.com/eranbo/react-native-base64) + +## 安装与使用 + +进入到工程目录并输入以下命令: + + + +#### **npm** + +```bash +npm i react-native-base64@0.2.1 + +// typescript 项目需下载对应包的类型声明 +npm i @types/react-native-base64 -D +``` + + +下面的代码展示了这个库的基本使用场景: + +```js +import React, { useState } from 'react'; +import base64 from 'react-native-base64'; +import { + ScrollView, + StyleSheet, + Text, + TextInput, + useColorScheme, + View, +} from 'react-native'; + +import { + Colors, +} from 'react-native/Libraries/NewAppScreen'; + +export function Base64Page(): JSX.Element { + const isDarkMode = useColorScheme() === 'dark'; + + // 输入文字 + const [word, setWord] = useState('react native'); + // 编码输入文字 + const encodeWord = base64.encode(word); + // 解码输入文字 + const decodeWord = base64.decode(encodeWord); + // 解码结果 是否 和原文字相同 + const wordEqualDecodeWord = `word equal decode word: ${word === decodeWord}` + // 将word 转为 unit8 array + const byteArrayWord = Uint8Array.from(word.split(''), w => w.charCodeAt(0)); + // 对 unit 8 array 进行编码 + const encodeWordFromByteArray = base64.encodeFromByteArray(byteArrayWord); + // 对 上一步结果进行解码 + const decodeFromByteArray = base64.decode(encodeWordFromByteArray); + + const backgroundStyle = { + backgroundColor: isDarkMode ? Colors.darker : Colors.lighter, + }; + + return ( + <> + + + + Test Word: + + encode Result: {encodeWord} + decode Result: {decodeWord} + encodeFromByteArray Result: {encodeWordFromByteArray} + decode the value above Result: {decodeFromByteArray} + + + + + ); + +} + +const styles = StyleSheet.create({ + cardView: { margin: 10, backgroundColor: '#f0f0f0', display: 'flex', padding: 10, borderRadius: 8 }, + TextInput: {height: 40, borderColor: '#ccc', borderWidth: 1, borderRadius: 4, width: '90%'}, + highlight: { + fontWeight: '700', + }, +}); + +``` + +## 兼容性 + +在以下版本验证通过: + + 1. SDK: HarmonyOS NEXT Devloper Preview1; + 测试设备: Mate60 (BRA-AL00); + Rom: 204.1.0.59(SP2DEVC00E60R4P1); + IDE: 4.1.3.500; + RNOH: 0.72.13 + + +### base64 提供的方法: + + +| Name | Description | Type | Required | HarmonyOS Support | +| ---- | ---- | ---- | -------- | -------- | +| encode| encode from string |string| No | Yes | +| decode | decode base64 strings | string | No | Yes | +| encodeFromByteArray | encode from Unit8Array | Unit8Array | No | Yes | + + +## 遗留问题 + +## 其他 + +## 开源协议 + +本项目基于 [MIT License](https://github.com/eranbo/react-native-base64/blob/master/LICENSE) ,请自由地享受和参与开源。