1
0
mirror of https://gitcode.com/gh_mirrors/re/react-native-pushy.git synced 2025-12-21 13:13:58 +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,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;