1
0
mirror of https://gitcode.com/gh_mirrors/re/react-native-pushy.git synced 2025-11-22 15:36:10 +08:00
Code Issues Packages Projects Releases Wiki Activity GitHub Gitee

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:
sunnylqm
2025-11-18 20:19:06 +08:00
parent 2a79061b89
commit e151c9c618
4 changed files with 20 additions and 2 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "react-native-update", "name": "react-native-update",
"version": "10.35.7", "version": "10.35.8",
"description": "react-native hot update", "description": "react-native hot update",
"main": "src/index", "main": "src/index",
"scripts": { "scripts": {

View File

@@ -1,6 +1,7 @@
import { createContext, useContext } from 'react'; import { createContext, useContext } from 'react';
import { CheckResult, ProgressData } from './type'; import { CheckResult, ProgressData } from './type';
import { Pushy, Cresc } from './client'; import { Pushy, Cresc } from './client';
import i18n from './i18n';
const noop = () => {}; const noop = () => {};
const asyncNoop = () => Promise.resolve(); const asyncNoop = () => Promise.resolve();
@@ -50,7 +51,16 @@ export const UpdateContext = createContext<{
lastError?: Error; lastError?: Error;
}>(defaultContext); }>(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 */ /** @deprecated Please use `useUpdate` instead */
export const usePushy = useUpdate; export const usePushy = useUpdate;

View File

@@ -71,4 +71,8 @@ export default {
// Development environment messages // Development environment messages
dev_incremental_update_disabled: 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.', '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={...}>.',
}; };

View File

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