mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-11-22 23:46:10 +08:00
Enhance useUpdate hook to validate context usage and add error messages for out-of-provider usage in English and Chinese locales.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { createContext, useContext } from 'react';
|
||||
import { CheckResult, ProgressData } from './type';
|
||||
import { Pushy, Cresc } from './client';
|
||||
import i18n from './i18n';
|
||||
|
||||
const noop = () => {};
|
||||
const asyncNoop = () => Promise.resolve();
|
||||
@@ -50,7 +51,16 @@ export const UpdateContext = createContext<{
|
||||
lastError?: Error;
|
||||
}>(defaultContext);
|
||||
|
||||
export const useUpdate = () => useContext(UpdateContext);
|
||||
export const useUpdate = __DEV__ ? () => {
|
||||
const context = useContext(UpdateContext);
|
||||
|
||||
// 检查是否在 UpdateProvider 内部使用
|
||||
if (!context.client) {
|
||||
throw new Error(i18n.t('error_use_update_outside_provider'));
|
||||
}
|
||||
|
||||
return context;
|
||||
} : () => useContext(UpdateContext);
|
||||
|
||||
/** @deprecated Please use `useUpdate` instead */
|
||||
export const usePushy = useUpdate;
|
||||
|
||||
@@ -71,4 +71,8 @@ export default {
|
||||
// Development environment messages
|
||||
dev_incremental_update_disabled:
|
||||
'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.',
|
||||
|
||||
// Context error messages
|
||||
error_use_update_outside_provider:
|
||||
'useUpdate must be used within an UpdateProvider. Please wrap your component tree with <UpdateProvider client={...}>.',
|
||||
};
|
||||
|
||||
@@ -68,4 +68,8 @@ export default {
|
||||
// Development environment messages
|
||||
dev_incremental_update_disabled:
|
||||
'当前是开发环境,无法执行增量式热更新,重启不会生效。如果需要在开发环境中测试可生效的全量热更新(但也会在再次重启后重新连接 metro),请打开"忽略时间戳"开关再重试。',
|
||||
|
||||
// Context error messages
|
||||
error_use_update_outside_provider:
|
||||
'useUpdate 必须在 UpdateProvider 内部使用。请使用 <UpdateProvider client={...}> 包裹您的组件树。',
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user