How to fix 'no fallback UI was specified' in react i18next using hooks

Mic*_*ell 4 reactjs react-i18next react-hooks react-suspense

I am trying to implement i18next in my react component using the useTranslation hook, but it keeps saying:

Uncaught Error: Test suspended while rendering, but no fallback UI was specified.

Add a <Suspense fallback=...> component higher in the tree to provide a loading indicator or placeholder to display.

我怎样才能添加<Suspense>比我拥有的更高的任何东西?我究竟做错了什么?我该如何解决?当我使用该<Translation>组件时,它似乎工作正常。当然,如果我关闭 Suspense 并尝试自己处理它似乎也可以正常工作,但我认为这违背了目的。我怎样才能使它真正起作用?我的 Fetch 后端是否配置错误?

测试.js

import React, { Suspense, useState, useEffect, lazy } from "react";
import PropTypes from "prop-types";

import i18n from './i18n';
import { useTranslation } from 'react-i18next';

export default function Test( props ){
  const { t, i18n } = useTranslation( 'bingo_game' );

    return (
    <Suspense fallback={<div>Loading...</div>}>
      <div>
        Pant leg
      </div>
    </Suspense>
    )
}

Test.propTypes = {
  token: PropTypes.string.isRequired,
};
Run Code Online (Sandbox Code Playgroud)

和

i18n.js

import i18n from "i18next";
import LngDetector from "i18next-browser-languagedetector";
import { initReactI18next } from "react-i18next";

import Fetch from "i18next-fetch-backend";

  i18n
    .use(LngDetector) //language detector
    .use(Fetch)
    .use(initReactI18next)
    .init({
      backend: {
        loadPath: "/infra/locales/{{ns}}.json",
        // path to post missing resources
        addPath: "locales/add/{{ns}}",
        // define how to stringify the data when adding missing resources
        stringify: JSON.stringify
      },
      defaultNS: "base",
      fallbackLng: "en",
      debug: true,
      initImmediate: false
    });

export default i18n;
Run Code Online (Sandbox Code Playgroud)

我已经浏览了我可以找到的文档以及这篇不是我的问题的 SO 帖子。

小智 7

在您的i18n文件中添加此内容,它将发挥作用:)

.init({
   react: {
     useSuspense: false,
     ...

  });
Run Code Online (Sandbox Code Playgroud)


小智 4

这不是最好的方法,但我可以建议您通过将此行添加到您的配置中来关闭悬念

i18n
  .init({
  ...
  // react i18next special options (optional)
  react: {
    useSuspense: false,
    wait: false,
  },
});
Run Code Online (Sandbox Code Playgroud)

  • 是的,我尝试过并且有效,但我不想禁用该功能,我希望它能够正常工作。 (5认同)