简体   繁体   English

Object 动态传递给 React 组件给出 Typescript 错误

[英]Object passed dynamically to React component gives Typescript error

I'm having a typescript error when trying to pass some dynamic values to a React component:尝试将一些动态值传递给 React 组件时,我遇到了 typescript 错误:

Property 'title' does not exist on type 'string'.ts(2339)类型“字符串”上不存在属性“标题”.ts(2339)

 import { useTranslation } from "react-i18next";
import OurMissionList from "./SubModules/our-mission-list.component";

export const OurMission = () => {
  const { t } = useTranslation();
  const section = t(`ourmission:section`, {
    returnObjects: true
  });

  console.log("section", section);

  return (
    <div className="our-mission content">
      <div className="section_info_wrapper container">
        <div className="info_text">
          <h2>{section.title}</h2>
          <p className="main_text"> {section.text}</p>
          <h3>{section.subtitle}</h3>
          <OurMissionList />
        </div>
      </div>
    </div>
  );
};
export default OurMission;

The console.log of section is:部分的 console.log 是:

    {
    "title": "Deliver the best possible experience",
    "text": "To be the first choice of consumers and businesses by providing them with the best possible experience in telecommunications, entertainment, news media and culture, while being a leader in each of our lines of business.",
    "subtitle": "Key behaviours",
    "list": [
        {
            "name": "Performance",
            "pos": "Give your best effort at all times and dare to press for success."
        },
        {
            "name": "Innovation",
            "pos": "Come up with new ideas to solve the problems at hand and set us apart."
        },
        {
            "name": "Customer focus",
            "pos": "Be fully engaged, listen and demonstrate professionalism in order to anticipate and meet the wants and needs of our customers and business partners."
        },
        {
            "name": "Teamwork",
            "pos": "Provide mutual support and take advantage of each colleague’s strengths and ideas."
        },
        {
            "name": "Agility",
            "pos": "Be flexible and ready to adapt to circunstances."
        }
    ]
}

Anybody knows how to fix it?有人知道如何解决吗? Thanks!谢谢!

Looks like you'll have to add a declaration file that extends some interfaces from the react_i18next package with types gathered from your translation JSON files for the t function to return the proper types.看起来您必须添加一个声明文件,该文件从 react_i18next package 扩展一些接口,其中类型从您的翻译t文件中收集,用于返回正确的类型。

This is explained here: https://react.i18next.com/latest/typescript#create-a-declaration-file这在这里解释: https://react.i18next.com/latest/typescript#create-a-declaration-file

In short:简而言之:

// react-i18next.d.ts
import 'react-i18next';
// import all namespaces (for the default language, only)
import ns1 from 'locales/en/ns1.json';
import ns2 from 'locales/en/ns2.json';

// react-i18next versions lower than 11.11.0
declare module 'react-i18next' {
  interface Resources {
    ns1: typeof ns1;
    ns2: typeof ns2;
  }
}

// react-i18next versions higher than 11.11.0
declare module 'react-i18next' {
  interface CustomTypeOptions {
    // custom namespace type if you changed it
    defaultNS: 'ns1';
    resources: {
      ns1: typeof ns1;
      ns2: typeof ns2;
    };
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeScript 在 React 组件中导入 object 后出现“对象可能未定义”错误 - TypeScript gives 'Object is possibly undefined' error after importing object in React component Socketio object 在 React 中传递给子组件时未定义,typescript - Socketio object undefined when it's passed to child component in React, typescript 作为 prop 传入的 function 的 React 组件抛出 TypeScript 错误 - React component throwing TypeScript error for function passed in as prop React + Typescript对象支持组件错误 - React + Typescript object props to component error 将具有给定属性的对象值显示为子项会产生错误 - React 和 Typescript - Displaying object values with given properties as children gives error - React and Typescript Typescript 接口用于 Annoymous Function 传递到 React 组件 - Typescript Interface for Annoymous Function passed into React Component React &amp; TypeScript 使用通过 props 传递的组件 - React & TypeScript usage of component passed via props 将子项注入到 TypeScript 中由 props 传递的反应组件中 - Inject children into react component passed by props in TypeScript 将 React Typescript 组件转换为 Javascript 给出错误 - Conversion React Typescript component into Javascript gives errors Typescript 验证从传递给通用 React 组件的回调返回的 object 的属性? - Typescript validation for properties of an object returned from a callback passed to a generic React component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM