Skip to content

Commit e151c9c

Browse files
committed
Enhance useUpdate hook to validate context usage and add error messages for out-of-provider usage in English and Chinese locales.
1 parent 2a79061 commit e151c9c

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-update",
3-
"version": "10.35.7",
3+
"version": "10.35.8",
44
"description": "react-native hot update",
55
"main": "src/index",
66
"scripts": {

src/context.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createContext, useContext } from 'react';
22
import { CheckResult, ProgressData } from './type';
33
import { Pushy, Cresc } from './client';
4+
import i18n from './i18n';
45

56
const noop = () => {};
67
const asyncNoop = () => Promise.resolve();
@@ -50,7 +51,16 @@ export const UpdateContext = createContext<{
5051
lastError?: Error;
5152
}>(defaultContext);
5253

53-
export const useUpdate = () => useContext(UpdateContext);
54+
export const useUpdate = __DEV__ ? () => {
55+
const context = useContext(UpdateContext);
56+
57+
// 检查是否在 UpdateProvider 内部使用
58+
if (!context.client) {
59+
throw new Error(i18n.t('error_use_update_outside_provider'));
60+
}
61+
62+
return context;
63+
} : () => useContext(UpdateContext);
5464

5565
/** @deprecated Please use `useUpdate` instead */
5666
export const usePushy = useUpdate;

src/locales/en.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,8 @@ export default {
7171
// Development environment messages
7272
dev_incremental_update_disabled:
7373
'Currently in development environment, incremental hot update cannot be executed and restart will not take effect. If you need to test effective full hot update in development environment (but will reconnect to metro after restart), please enable "ignore timestamp" switch and retry.',
74+
75+
// Context error messages
76+
error_use_update_outside_provider:
77+
'useUpdate must be used within an UpdateProvider. Please wrap your component tree with <UpdateProvider client={...}>.',
7478
};

src/locales/zh.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,8 @@ export default {
6868
// Development environment messages
6969
dev_incremental_update_disabled:
7070
'当前是开发环境,无法执行增量式热更新,重启不会生效。如果需要在开发环境中测试可生效的全量热更新(但也会在再次重启后重新连接 metro),请打开"忽略时间戳"开关再重试。',
71+
72+
// Context error messages
73+
error_use_update_outside_provider:
74+
'useUpdate 必须在 UpdateProvider 内部使用。请使用 <UpdateProvider client={...}> 包裹您的组件树。',
7175
};

0 commit comments

Comments
 (0)