简体   繁体   English

反应 typescript 句柄 JSX.Element 或字符串

[英]React typescript handle JSX.Element or string

I have a payload that looks like this:我有一个看起来像这样的有效负载:

{
  age: 35,
  comments: "Walk in appointments",
  dateTime: {
  content: {
    $$typeof: Symbol(react.element),
    key: null,
    props: {
      children: "30-Aug-2021, 03:35 PM",
      className: "-esm-appointments__appointments-table__statusContainer___RFuGX"
    },
    ref: null,
    type: "span",
  },
  dob: "03 — Apr — 1986",
  gender: "M",
  id: "7cd38a6d-377e-491b-8284-b04cf8b8c6d8",
  location: "HIV Clinic",
  name: {
    content: {
    $$typeof: Symbol(react.element),
    key: null,
    props: {
    children: "John Wilson",
    to: "${openmrsSpaBase}/patient/8673ee4f-e2ab-4077-ba55-4980f408773e/chart"
  },
  ref: null,
  type: ƒ ConfigurableLink(_param),
  _self: null,
  patientUuid: "8673ee4f-e2ab-4077-ba55-4980f408773e",
  phoneNumber: "0700000000",
  provider: "Dr James Cook",
  visitType: "HIV Clinic"
}

It contains data from table rows, which are formatted like this:它包含来自表格行的数据,其格式如下:

const tableRows = useMemo(() => {
    return appointments?.map((appointment) => ({
      ...appointment,
      name: {
        content: (
          <ConfigurableLink to={`\${openmrsSpaBase}/patient/${appointment.patientUuid}/chart`}>
            {appointment.name}
          </ConfigurableLink>
        ),
      },
  }, [appointments]);

I am trying to create an interface for appointments data, which should handle data from different sources, some are JSX elements and some are strings.我正在尝试为约会数据创建一个接口,它应该处理来自不同来源的数据,有些是 JSX 元素,有些是字符串。

I am having a hard time creating an interface that can handle either a JSX.Element or string:我很难创建一个可以处理 JSX.Element 或字符串的接口:

export interface MappedAppointment {
  id: string;
  name: { content: JSX.Element } | string;
  age: string;
  gender: string;
  phoneNumber: string;
  dob: string;
  patientUuid: string;
  dateTime: { content: JSX.Element } | string;
  serviceType: { content: JSX.Element } | string;
}

I keep getting the error Property 'content' does not exist on type 'string | { content: Element; }'.Property 'content' does not exist on type 'string'.我不断收到错误Property 'content' does not exist on type 'string | { content: Element; }'.Property 'content' does not exist on type 'string'. Property 'content' does not exist on type 'string | { content: Element; }'.Property 'content' does not exist on type 'string'. . . This is my view:这是我的看法:

<span className={styles.label}>{appointment.name.content.props.children}</span> <br></br>

Any advise will be appreciated.任何建议将不胜感激。 Thanks谢谢

You can check the typeof of name property before accessing the property of it.您可以在访问它的属性之前检查 name 属性的typeof If is not string type then access the content property on it.如果不是字符串类型,则访问其上的内容属性。

typeof appointment.name === "string"? appointment.name: appointment.name.content.props.children

Full code:完整代码:

<span className={styles.label}>
    {typeof appointment.name === "string" ? appointment.name : appointment.name.content.props.children}
</span> 
<br></br>

You can read about type guarding using various patterns at this Typescript documentation.您可以在Typescript 文档中阅读有关使用各种模式的类型保护的信息。

Similar to the previous answer.与上一个答案类似。 You can try an Elvis operator and check that the property exists.您可以尝试使用 Elvis 运算符并检查该属性是否存在。

Like:喜欢:

<span className={styles.label}>
{(appointment.name.content && appointment.name.content.props && appointment.name.content.props.children) ? 
  appointment.name.content.props.children :
  appointment.name
}
</span> 
<br></br>

name.content is an object, not a JSX element. name.content是 object,而不是 JSX 元素。 What you mean to type is你的意思是输入是

name: {
  content: {
    $$typeof: JSX.Element | string;
    ...other properties
  }
};

That said, this response data looks tricky to me due to object keys/value such as type: ƒ ConfigurableLink(_param) , _self and $$typeof .也就是说,由于 object 键/值(例如type: ƒ ConfigurableLink(_param)_self$$typeof ),这个响应数据对我来说看起来很棘手。 I'd recommend not using such keys as javascript may have issues reading them.我建议不要使用诸如 javascript 之类的键,读取它们时可能会遇到问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM