diff --git a/1224/Immer.md b/1224/Immer.md
new file mode 100644
index 0000000000000000000000000000000000000000..e6f6f82fbff62270ffc132beb33e1224befb1d92
--- /dev/null
+++ b/1224/Immer.md
@@ -0,0 +1,810 @@
+模板版本:v0.1.3
+
+
+
immer
+
+
+
+
+
+
+
+
+
+> [!TIP] [Github 地址](https://github.com/immerjs/immer)
+
+## 安装与使用
+
+进入到工程目录并输入以下命令:
+
+>[!TIP] # 处替换为 tgz 包的路径
+
+
+
+#### **npm**
+
+```bash
+npm immer
+```
+
+#### **yarn**
+
+```bash
+yarn add immer
+```
+
+
+
+下面的代码展示了这个库的基本使用场景:
+
+```js
+import React, {useState, useEffect, Component} from 'react';
+import {
+ View,
+ Text,
+ Button,
+ StyleSheet,
+ ScrollView,
+ Switch,
+ TextInput,
+} from 'react-native';
+import {
+ createDraft,
+ finishDraft,
+ current,
+ produce,
+ original,
+ isDraft,
+ enablePatches,
+ produceWithPatches,
+ enableMapSet,
+ freeze,
+ nothing,
+ setAutoFreeze,
+ isDraftable,
+ immerable,
+ Immutable,
+ castImmutable,
+ castDraft,
+ applyPatches,
+ setUseStrictShallowCopy,
+} from 'immer';
+
+// 初始状态
+const initialState = {
+ count: 0,
+ text: 'Hello from Immer!',
+};
+enablePatches();
+
+// 启用enablePatches插件
+enableMapSet();
+// 启用enableMapSet插件
+setUseStrictShallowCopy(true);
+
+const MyComponent = () => {
+ const [myMap, setMyMap] = useState(new Map([['key1', 'value1']]));
+ const [overStates, setOverStates] = useState(false);
+ const toggleSwitch = value => {
+ setOverStates(value);
+ };
+ let [states, setStates] = useState(false);
+ const [originals, setOriginals] = useState({users: [{name: 'zhansan'}]});
+ let [res, setRes] = useState('');
+ let [currentText, setCurrent] = useState('');
+ const [count, setCount] = useState({age: 0});
+ let [baseState, setBaseState] = useState([
+ {
+ title: '标题1',
+ done: true,
+ },
+ ]);
+ const [text, setText] = useState('Hello');
+ let [result, setResult] = useState({});
+ let [patches, setPatches] = useState({});
+ let [inversePatches, setInversePatches] = useState({});
+ const [data, setData] = useState({name: 'John', age: 30});
+ const [produceStatus, setProduceStatus] = useState(false);
+ const [castDraftState, setCastDraftState] = useState({
+ count: 0,
+ });
+ const [state, setState] = useState(initialState);
+ const [freezeRest, setFreezeRest] = useState({a: 1, b: {c: 2}});
+ const [stateRes, setStateRes] = useState({count: 0, text: 'hello-world'});
+ const [texts, setTexts] = useState('请输入内容');
+ const [stateNothing, setStateNothing] = useState({
+ name: 'John',
+ age: 30,
+ isStudent: false,
+ });
+ const obj = {a: 1, b: 2};
+ const arr = [1, 2, 3];
+ const str = 'Hello, immer!';
+ const num = 42;
+ const frozenObj = freeze({c: 3}, true);
+ const [objStatus, setObjStatus] = useState(false);
+ const [arrStatus, setArrStatus] = useState(false);
+ const [strStatus, setStrStatus] = useState(false);
+ const [numStatus, setNumStatus] = useState(false);
+ const [draftsStatus, setDratfStatus] = useState(false);
+ let originalRes = {
+ name: '初始数据1',
+ age: 18,
+ };
+ let fork = originalRes;
+ // 用户在向导中所作的所有更改
+ let changes = [];
+ // 与向导中所做的所有更改相反
+ let inverseChanges = [];
+ let [obj1, setObj1] = useState({});
+ let [obj2, setObj2] = useState({});
+ const incrementCount = () => {
+ if (count.age < 3) {
+ setCount(
+ produce(count, draft => {
+ draft.age += 1;
+ }),
+ );
+ } else {
+ setCount(
+ produce(count, draft => {
+ draft.age = 0;
+ }),
+ );
+ }
+ };
+ const decrement = () => {
+ if (count.age > 0) {
+ setCount(
+ produce(count, draft => {
+ draft.age -= 1;
+ }),
+ );
+ } else {
+ setCount(
+ produce(count, draft => {
+ draft.age = 3;
+ }),
+ );
+ }
+ };
+ const onAdd = () => {
+ if (baseState.length < 3) {
+ return setBaseState(
+ produce(baseState, draftState => {
+ draftState.push({title: '新增', done: states});
+ setStates((states = isDraft(draftState)));
+ }),
+ );
+ }
+ };
+ const onDelete = (e: number) => {
+ return setBaseState(
+ produce(draft => {
+ draft.splice(e, 1);
+ }),
+ );
+ };
+ const onChange = (item: object, e: number) => {
+ const res = produce(baseState, draftState => {
+ draftState[e].done = !draftState[e].done;
+ draftState[e].title = '更改后数据';
+ setStates((states = isDraft(draftState)));
+ });
+ setBaseState(res);
+ };
+ // 使用createDraft来创建一个草稿副本
+ const increment = () => {
+ const draft = createDraft(state);
+ if (state.count < 5) {
+ draft.count += 1; // 修改草稿副本
+ } else {
+ draft.count = 0;
+ }
+ const nextState = finishDraft(draft); // 获取修改后的不可变状态
+ setState(nextState); // 更新React状态
+ };
+ const updateMap = () => {
+ setMyMap(
+ produce(myMap, draftMap => {
+ draftMap.set('key1', 'updatedValue1'); // 更新键值对
+ draftMap.set('key2', 'value2'); // 添加新的键值对
+ draftMap.delete('key3'); // 尝试删除一个不存在的键值对(不会有影响)
+ }),
+ );
+ };
+ const renderMapEntries = () => {
+ return Array.from(myMap.entries()).map(([key, value]) => (
+
+ {key}: {value}
+
+ ));
+ };
+ const changeText = () => {
+ const draft = createDraft(state);
+ draft.text = 'Text updated!'; // 修改草稿副本
+ const nextState = current(draft); // 获取当前修改后的状态(与finishDraft效果相同)
+ setState(nextState); // 更新React状态
+ };
+ const clickOriginal = () => {
+ const a = setOriginals(
+ produce(originals, draft => {
+ draft.users[0].name = '4444';
+ setCurrent(current(draft).users[0].name);
+ console.log('Original state:', original(draft)?.users[0].name); // 输出原始状态
+ setRes(original(draft).users[0].name);
+ }),
+ );
+ };
+ const handleClick = () => {
+ const [result, patches, inversePatches] = produceWithPatches(
+ text,
+ draft => (draft = 'Hello-World'),
+ );
+ setResult(result);
+ setPatches(patches);
+ setInversePatches(inversePatches);
+ console.log('result:', result);
+ console.log('Patches:', patches);
+ console.log('Inverse Patches:', inversePatches);
+
+ setText(result);
+ };
+ // -----------------------------------------------
+ class MyImmerableObject extends React.Component {
+ // 声明immerable属性,告诉immer处理哪些属性
+ static [immerable] = true;
+ // 定义您的对象的属性
+ constructor(name, age) {
+ super(name, age);
+ this.name = name;
+ this.age = age;
+ }
+ /* // 不使用immerable
+ // 声明immerable属性,告诉immer处理哪些属性
+ static [immerable] = true;
+ // 定义您的对象的属性
+ constructor(name, age) {
+ super(name, age);
+ this.name = name;
+ this.age = age;
+ } */
+ }
+ const [myState, setMyState] = useState(new MyImmerableObject('Alice', 10));
+ const updateValue = () => {
+ // 使用immerable
+ const res = produce(myState, draft => {
+ draft.age = 26;
+ });
+ /* // 不使用immerable
+ const res = {...myState, age: 26}; */
+ setMyState(res);
+ };
+ // 在组件挂载时设置 immer 的自动冻结行为
+ useEffect(() => {
+ // 启用/停用自动冻结
+ setAutoFreeze(overStates);
+ }, [overStates]); // 空依赖数组确保此 effect 只运行一次
+ const incrementCountLeft = () => {
+ const res = freeze(Object.assign({}, freezeRest), true);
+ res.a = 110;
+ res.b.c = 220;
+ console.log('调用freeze-deep:', overStates, res);
+ setFreezeRest(res);
+ };
+ const tryToModifyFrozenStateLeft = () => {
+ const res = freeze(Object.assign({}, freezeRest), false);
+ res.a = 111;
+ res.b.c = 222;
+ console.log('调用freeze-deep:', overStates, res);
+ setFreezeRest(res);
+ };
+ const ClickOffFreeze = () => {
+ const res1 = Object.assign({}, freezeRest);
+ res1.a = 10;
+ res1.b.c = 20;
+ console.log('未调用freeze:', res);
+ setFreezeRest(res1);
+ };
+ const ClickOnFreeze = () => {
+ const res = freeze(Object.assign({}, freezeRest));
+ res.a = 11;
+ res.b.c = 22;
+ console.log('调用freeze:', res);
+ setFreezeRest(res);
+ };
+ const inputText = e => {
+ let nextState = produce(stateRes, draft => {});
+ setTexts(e);
+ nextState.text = e;
+ setStateRes(nextState);
+ };
+ const clickNothing = e => {
+ const nextStateNothing = produce(stateNothing, draft => e);
+ setStateNothing(nextStateNothing);
+ };
+ const isDraftObj = () => {
+ setObjStatus(isDraftable(obj));
+ };
+ const isDraftArr = () => {
+ setArrStatus(isDraftable(arr));
+ };
+ const isDraftStr = () => {
+ setStrStatus(isDraftable(str));
+ };
+ const isDraftNum = () => {
+ setNumStatus(isDraftable(num));
+ };
+ const isDrafts = () => {
+ setDratfStatus(isDraftable(frozenObj));
+ };
+ // --------------------------------------------
+ const [draftRes, setDraftRes] = useState({
+ name: 'John',
+ age: 30,
+ hobbies: ['reading', 'coding'],
+ });
+
+ const [setUseStrictShallowCopyRes, setSetUseStrictShallowCopyRes] = useState({
+ name: 'John',
+ age: 30,
+ address: {
+ city: 'New York',
+ country: 'USA',
+ },
+ });
+ const [setUseStrictShallowCopyInit, setSetUseStrictShallowCopyInit] =
+ useState();
+ const updateData = () => {
+ let newData = {};
+ if (!produceStatus) {
+ newData = produce(data, draft => {
+ if (draft.age < 40) {
+ draft.age += 1;
+ } else {
+ draft.age = 0;
+ }
+ });
+ } else {
+ newData = castImmutable(data, draft => {
+ draft.age += 1;
+ });
+ }
+
+ setData(newData);
+ };
+ const Clickfn = () => {
+ setProduceStatus(!produceStatus);
+ };
+ const AddFn = () => {
+ setCastDraftState(
+ produce(castDraftState, draftState => {
+ if (castDraftState.count < 10) {
+ draftState.count += 1;
+ } else {
+ draftState.count = 0;
+ }
+ }),
+ );
+ };
+ const doubleCount = () => {
+ setCastDraftState(
+ produce(castDraftState, draftState => {
+ if (castDraftState.count < 100) {
+ draftState.count *= 2;
+ } else {
+ draftState.count = 0;
+ }
+ }),
+ );
+ };
+ const resetCount = () => {
+ const newState = castDraft({count: 0});
+ setCastDraftState(newState);
+ };
+ const ClickChange = () => {
+ fork = produce(
+ fork,
+ draft => {
+ draft.age = 10;
+ draft.name = 'lisi';
+ },
+ // 产生的第三个参数是一个回调,patches 将从这里产生
+ (patches, inversePatches) => {
+ changes.push(...patches);
+ inverseChanges.push(...inversePatches);
+ },
+ );
+ };
+ const fn1 = () => {
+ // 当向导完成(成功)后,我们可以将 fork 中的更改重播到新的状态!
+ let originalRes2 = applyPatches(originalRes, changes);
+ setObj1(originalRes2);
+ console.log('originalRes2:', originalRes2);
+ };
+ const fn2 = () => {
+ // state 现在包含来自两个代码路径的更改!
+ // 最后,即使在完成向导之后,用户也可能会改变主意并撤消他的更改......
+ let originalRes3 = applyPatches(originalRes, inverseChanges);
+ setObj2(originalRes3);
+ };
+ const handleAddHobby = () => {
+ //不使用Draft
+ const newDraftRes = {...draftRes};
+ newDraftRes.hobbies[1] = 'swimming';
+ setDraftRes(newDraftRes);
+ // 使用Draft
+ /* const nextDraftRes = produce(draftRes, draft => {
+ draft.hobbies[1] = 'swimming';
+ });
+ setDraftRes(nextDraftRes); */
+ };
+ interface Person {
+ name: string;
+ age: number;
+ }
+ // 使用Immutable
+ const immutableObj1: Immutable = {
+ name: 'Alice',
+ age: 20,
+ };
+ // 不使用Immutable
+ // const immutableObj1 = {
+ // name: 'Alice',
+ // age: 20,
+ // };
+ const [immutableData, setImmutableData] = useState();
+ const changeObj1 = () => {
+ const res = immutableObj1;
+ res.name = 'lisi';
+ console.log('immutableObj1:', immutableObj1);
+ setImmutableData(res);
+ };
+ const useSetUseStrictShallowCopy = () => {
+ const res = produce(setUseStrictShallowCopyRes, draft => {
+ draft.age = 55;
+ draft.address.city = 'China';
+ });
+ console.log('res:', JSON.stringify(res));
+ console.log(
+ 'setSetUseStrictShallowCopyRes:',
+ JSON.stringify(setUseStrictShallowCopyRes),
+ );
+ setSetUseStrictShallowCopyInit(res);
+ };
+
+ // ----------------分割 end-------------
+ return (
+
+
+ Immer
+ {/* 验证---produce---api */}
+
+ 验证-produce-期望值不可变数据可以更改
+
+
+ Count: {count.age}
+
+
+
+
+ {baseState.map((item, index) => (
+
+
+ 标题:{item.title}
+ 状态:{item.done ? 'true' : 'false'}
+ isDraft状态:{`${states}`}
+
+
+
+
+ ))}
+
+
+ {/* --------验证----createDraft--&--finishDraft---------------- */}
+
+ 验证createDraft--&--finishDraft
+ Count: {state.count}
+ Text: {state.text}
+
+
+
+ {/* -----------------验证--original-------current--------------- */}
+
+
+ 验证original--current--期望值验证original:获取修改后的原始状态
+
+ 初始数据:{JSON.stringify(originals)}
+ original:{res}
+ res:{originals.users[0].name}
+ current: {currentText}
+
+
+ {/* ------enablePatches-----produceWithPatches------------------ */}
+
+ 验证enablePatches--produceWithPatches
+ {text}
+ result:{JSON.stringify(result)}
+ patches:{JSON.stringify(patches)}
+ inversePatches:{JSON.stringify(inversePatches)}
+
+
+ {/* ----------验证--enableMapSet------------------------------- */}
+
+ 验证enableMapSet
+ Current Map:
+ {renderMapEntries()}
+
+
+ {/* -----------------验证--freeze----------------------- */}
+
+ 验证freeze
+
+ freeze(obj,deep?)obj:要冻结的对象deep(可选):一个布尔值,默认为false。如果为true,则深度冻结对象(包括其所有嵌套的对象)。如果为false或未提供,则只冻结对象的顶层属性
+
+
+
+ init:{JSON.stringify(freezeRest)}
+
+
+
+
+
+
+
+ {/* --------------验证--setAutoFreeze------------------- */}
+
+ ---验证setAutoFreeze----
+
+ setAutoFreeze为自动冻结,状态:{JSON.stringify(overStates)}
+
+
+
+ 你输入的内容是: {stateRes.text}
+ Current Count: {JSON.stringify(stateRes)}
+
+ {/* --------------验证---nothing------------------------ */}
+
+ 验证nothing
+
+ 定义的数据:
+ {JSON.stringify(stateNothing)}
+
+ Name: {typeof stateNothing}
+
+
+ {
+ clickNothing(nothing);
+ }}
+ />
+
+
+ {
+ clickNothing(undefined);
+ }}
+ />
+
+
+
+ {/* -----------验证----isDraftable---------------------- */}
+
+ 验证isDraftable
+ {/*
+ {result ? '可变对象' : '不可变对象'} */}
+
+ 状态--isDraftable({JSON.stringify(obj)})
+
+ 状态:{JSON.stringify(objStatus)}
+
+
+ 状态--isDraftable({JSON.stringify(arr)})
+
+ 状态--{JSON.stringify(arrStatus)}
+
+
+ 状态--isDraftable({JSON.stringify(str)})
+
+ 状态--{JSON.stringify(strStatus)}
+
+
+ 状态--isDraftable({JSON.stringify(num)})
+
+ 状态--{JSON.stringify(numStatus)}
+
+
+ 状态--isDraftable({JSON.stringify(frozenObj)})
+
+ 状态--{JSON.stringify(draftsStatus)}
+
+
+ {/* -----------验证---immerable-------------------- */}
+
+
+ 验证---immerable,可以添加到构造函数或原型的符号
+
+ NUM:{JSON.stringify(myState)}
+
+
+ {/* -----------------验证--castImmutable----------------------- */}
+
+ 验证castImmutable
+ castImmutable,期望值castImmutable为true时数据不可变
+ castImmutable:{produceStatus ? 'true' : 'false'}
+
+ Age: {data.age}
+
+
+ {/* -------------------验证--castDraft--- */}
+
+ 验证castDraft
+
+ castDraft,期望将任何不可变类型转换为其可变对应物。这只是一个转换,实际上并没有做任何事情
+
+ Count: {castDraftState.count}
+
+
+
+
+ {/* ---------------验证--applyPatches-------------- */}
+
+ 验证applyPatches
+ 初始数据:{JSON.stringify(originalRes)}
+
+
+ 改变后数据:{JSON.stringify(obj1)}
+
+ 恢复的数据--{JSON.stringify(obj2)}
+
+
+
+ 验证Draft暴露的 TypeScript 类型以将不可变类型转换为可变类型
+
+ {JSON.stringify(draftRes)}
+
+
+ {/* 验证Immutable */}
+ {/*
+
+ 验证Immutable暴露的 TypeScript 类型以将可变类型转换为不可变类型
+
+ 初始数据:{JSON.stringify(immutableObj1)}
+ 更改后数据:{JSON.stringify(immutableData)}
+
+ */}
+ {/* 验证setUseStrictShallowCopy */}
+
+
+ 验证setUseStrictShallowCopy
+ 可用于启用严格浅拷贝,如果启用,immer会确保不会意外的修改原始对象
+
+ 初始数据:{JSON.stringify(setUseStrictShallowCopyRes)}
+ 更改后数据:{JSON.stringify(setUseStrictShallowCopyInit)}
+
+
+
+
+ );
+};
+
+const styles = StyleSheet.create({
+ container: {
+ width: '100%',
+ justifyContent: 'center',
+ alignItems: 'center',
+ padding: 10,
+ marginBottom: 5,
+ borderWidth: 1,
+ },
+ text: {
+ width: '100%',
+ fontSize: 18,
+ marginBottom: 0,
+ textAlign: 'center',
+ fontWeight: '700',
+ margin: 5,
+ },
+ border: {
+ borderWidth: 1,
+ },
+});
+
+export default MyComponent;
+
+
+```
+
+## 兼容性
+
+在以下版本验证通过
+
+1. RNOH:0.72.13; SDK:HarmonyOS NEXT Developer Preview1; IDE:DevEco Studio 4.1.3.500; ROM:204.1.0.59;
+
+## API
+
+详情见[Immer](https://immerjs.github.io/immer/zh-CN/api/)
+
+> | Name | Description | Required | Platform | HarmonyOS Support |
+> | ------------------------- | ------------------------------------------------------------ | -------- | ----------- | ----------------- |
+> | `(default)` | Immer 核心 API,通常命名为 `produce`: `import {produce} from "immer"` | NO | Android IOS | YES |
+> | `applyPatches` | 给定一个基本 state 或 draft,以及一组 patches ,应用 patches | NO | Android IOS | YES |
+> | `castDraft` | 将任何不可变类型转换为其可变对应物。这只是一个转换,实际上并没有做任何事情。 | NO | Android IOS | YES |
+> | `castImmutable` | 将任何可变类型转换为其不可变对应物。这只是一个转换,实际上并没有做任何事情。 | NO | Android IOS | YES |
+> | `createDraft` | 给定一个基本 state,创建一个可变 draft,任何修改都将被记录下来 | NO | Android IOS | YES |
+> | `current` | 给定一个 draft 对象(不必是对象的根结点),对 draft 的当前状态进行快照 | NO | Android IOS | YES |
+> | `Draft` | 暴露的 TypeScript 类型以将不可变类型转换为可变类型 | NO | Android IOS | YES |
+> | `enableMapSet()` | 启用对 `Map` 和 `Set` 集合的支持。 | NO | Android IOS | YES |
+> | `enablePatches()` | 启用对 JSON patches 的支持 | NO | Android IOS | YES |
+> | `finishDraft` | 给定使用 `createDraft` 创建的 draft,冻结 draft 并生成并返回下一个不可变状态,该状态捕获所有更改 | NO | Android IOS | YES |
+> | `freeze(obj, deep?)` | 冻结可 draft 对象。返回原始对象。默认情况下浅冻结,但如果第二个参数为真,它将递归冻结。 | NO | Android IOS | YES |
+> | `Immer` | 可用于创建第二个“immer”实例(暴露此实例中列出的所有 API)的构造函数,它不与全局实例共享其设置 | NO | Android IOS | YES |
+> | `immerable` | 可以添加到构造函数或原型的符号,表示 Immer 应该将类视为可以安全 draft 的东西 | NO | Android IOS | YES |
+> | `Immutable` | 暴露的 TypeScript 类型以将可变类型转换为不可变类型 | NO | Android IOS | YES |
+> | `isDraft` | 如果给定对象是 draft 对象,则返回 true | NO | Android IOS | YES |
+> | `isDraftable` | 如果 Immer 能够将此对象变成 draft,则返回 true。这适用于:数组、没有原型的对象、以 `Object` 为原型的对象、在其构造函数或原型上具有 `immerable` 符号的对象 | NO | Android IOS | YES |
+> | `nothing` | 可以从 recipe 返回的值,以指示应生成 `undefined` | NO | Android IOS | YES |
+> | `original` | 给定一个 draft 对象(不必是对象的根结点),返回原始状态树中相同路径的原始对象(如果存在) | NO | Android IOS | YES |
+> | `Patch` | 暴露的 TypeScript 类型,描述(反向)patches 对象的形状 | NO | Android IOS | YES |
+> | `produce` | Immer 的核心 API,也暴露为 `default` 导出 | NO | Android IOS | YES |
+> | `produceWithPatches` | 与 `produce` 相同,但它不仅返回生成的对象,还返回一个由 `[result, patch, inversePatches]` 组成的元组 | NO | Android IOS | YES |
+> | `setAutoFreeze` | 启用/禁用递归的自动冻结。默认启用 | NO | Android IOS | YES |
+> | `setUseStrictShallowCopy` | 可用于启用严格浅拷贝,如果启用,immer将尽可能多地复制不可枚举属性 | NO | Android IOS | YES |
+
+## 遗留问题
+
+## 其他
diff --git a/1224/react-lifecycles-compat.md b/1224/react-lifecycles-compat.md
new file mode 100644
index 0000000000000000000000000000000000000000..0ee89e300e1072855fa134879dd21d93ea6ad51a
--- /dev/null
+++ b/1224/react-lifecycles-compat.md
@@ -0,0 +1,130 @@
+> 模板版本:v0.1.3
+
+
+
react-lifecycles-compat
+
+
+
+
+
+
+
+
+> [!TIP] [Github 地址](https://github.com/reactjs/react-lifecycles-compat)
+
+## 安装与使用
+
+进入到工程目录并输入以下命令:
+
+>[!TIP] # 处替换为 tgz 包的路径
+
+
+
+#### **npm**
+
+```bash
+npm install react-lifecycles-compat@3.0.4
+```
+
+#### **yarn**
+
+```bash
+yarn add react-lifecycles-compat@3.0.4
+```
+
+
+
+下面的代码展示了这个库的基本使用场景:
+
+```js
+import React from 'react';
+import {
+ Text,
+ View,
+ Button
+} from 'react-native';
+import { polyfill } from 'react-lifecycles-compat';
+import { State } from 'react-native-gesture-handler';
+class ShowComponent extends React.Component {
+ render() {
+ return (
+
+ 新组件
+
+ )
+ }
+}
+class ExampleComponent extends React.Component {
+ state = {
+ Text1: '未执行',
+ count1: 0,
+ Text2: '未执行',
+ visible: false
+ };
+ static getDerivedStateFromProps = (nextProps, prevState) => {
+ // Normally this method would only work for React 16.3 and newer,
+ // But the polyfill will make it work for older versions also!
+ return { Text1: '已执行', count1: prevState.count1 + 1 }
+ }
+
+ getSnapshotBeforeUpdate(prevProps, prevState) {
+ // Normally this method would only work for React 16.3 and newer,
+ // But the polyfill will make it work for older versions also!
+ return true
+ }
+ componentDidUpdate(prevProps, prevState, snapshot) {
+ if (snapshot) {
+ if (this.state.Text2 !== '已执行') {
+ this.setState({
+ Text2: '已执行'
+ })
+ }
+
+ }
+ }
+ // render() and other methods ...
+ handleClick = () => {
+ this.setState({ visible: true })
+ }
+ render() {
+ const { visible, Text1, Text2, count1 } = this.state
+ return (
+
+ {visible ? : {visible}}
+ getDerivedStateFromProps生命周期会在React初始化挂载和后续更新时调用render之前调用,返回一个对象来更新state,或者返回null就不更新任何内容
+ getSnapshotBeforeUpdate生命周期会在React更新DOM之前时直接调用,使你的组件能够在DOM发生更改之前捕获一些信息
+
+ 生命周期getDerivedStateFromProps{this.state.Text1}+{this.state.count1}
+ 生命周期getSnapshotBeforeUpdate{this.state.Text2}
+
+ )
+ }
+
+}
+
+// Polyfill your component so the new lifecycles will work with older versions of React:
+polyfill(ExampleComponent);
+
+export default ExampleComponent;
+```
+
+### 兼容性
+
+在以下版本验证通过
+
+1. RNOH:0.72.13; SDK:HarmonyOS NEXT Developer Preview1; IDE:DevEco Studio 4.1.3.500; ROM:204.1.0.59;
+
+| Name | Description | Required | Platform | HarmonyOS Support |
+| ------------------------ | -------------------------------------- | -------- | -------- | ----------------- |
+| getDerivedStateFromProps | Initialize the mount. | NO | Android | YES |
+| getSnapshotBeforeUpdate | Call directly before updating the DOM. | NO | Android | YES |
+
+## 遗留问题
+
+## 其他
+
+以下事项与原库保持一致需注意遵循:
+
+为了使polyfill工作,您的组件不能定义以下生命周期:componentWillMount,componentWillReceiveProps,componentWillUpdate.
+
+如果您的组件包含getSnapshotBeforeUpdate,那么也必须定义componentDidUpdate,如果不满足上述任何条件,将抛出错误.