簡體   English   中英

如何修復 NativeModule.RNLocalize 為 null?

[英]How to fix NativeModule.RNLocalize is null?

我正在使用react-native-localization庫是我的 RN 項目。

我的 RN 版本是0.59.4

我已經讓項目按預期在 android 上運行,但問題在於IOS構建。

npm安裝了react-native-localizationreact-native-localize並按照 github 手冊中的描述使用 pod 鏈接它們。

我做了我能做的一切,從鏈接到清理和多次構建項目。

但是在運行react-native-localize NativeModule.RNLocalize is null. To fix this issue try these steps react-native-localize NativeModule.RNLocalize is null. To fix this issue try these steps ,我按照控制台告訴我的操作進行操作,但徒勞無功。

有人可以告訴我我做錯了什么嗎?

創建一個像這樣的模擬文件(在根目錄中):

__mocks__/react-native-localize.js

檢查__mock__是否有兩個下划線。

這是文件的一個示例:

const getLocales = () => [
  // you can choose / add the locales you want
  { countryCode: "US", languageTag: "en-US", languageCode: "en", isRTL: false },
  { countryCode: "FR", languageTag: "fr-FR", languageCode: "fr", isRTL: false },
];

// use a provided translation, or return undefined to test your fallback
const findBestAvailableLanguage = () => ({
  languageTag: "en-US",
  isRTL: false,
});

const getNumberFormatSettings = () => ({
  decimalSeparator: ".",
  groupingSeparator: ",",
});

const getCalendar = () => "gregorian"; // or "japanese", "buddhist"
const getCountry = () => "US"; // the country code you want
const getCurrencies = () => ["USD", "EUR"]; // can be empty array
const getTemperatureUnit = () => "celsius"; // or "fahrenheit"
const getTimeZone = () => "Europe/Paris"; // the timezone you want
const uses24HourClock = () => true;
const usesMetricSystem = () => true;

const addEventListener = jest.fn();
const removeEventListener = jest.fn();

export {
  findBestAvailableLanguage,
  getLocales,
  getNumberFormatSettings,
  getCalendar,
  getCountry,
  getCurrencies,
  getTemperatureUnit,
  getTimeZone,
  uses24HourClock,
  usesMetricSystem,
  addEventListener,
  removeEventListener,
};

你不必導入react-native-localizationnode_module因為__mocks__下的每個文件都會被自動模擬。

嘗試再次運行測試並檢查錯誤是否仍然存在。

編輯:就我而言,我需要從react-native-localize獲得的唯一 function 是uses24HourClock()所以我的模擬文件很短:

const uses24HourClock = () => false;

export { uses24HourClock };

這就是我的全部。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM