【问题标题】:Is there any downside to using .jsx instead of .tsx all the times in ionic-react?在 ionic-react 中一直使用 .jsx 而不是 .tsx 有什么缺点吗?
【发布时间】:2023-03-22 05:25:02
【问题描述】:

如果我们在 ionic-react 中编写 jsx 语法会发生什么?是 state={} 对象和 props 在 .tsx 文件中有效。我通过使用 .jsx 文件而不是 .tsx 创建前端,它会不会在部署中造成麻烦,如果是这样的话。我如何在不依赖打字稿的情况下克服这个问题?

import React, { Component } from "react";
import { IonGrid, IonRow, IonCol, IonButton } from "@ionic/react";
import LocationComp from "../comon/locationComp";

class Tab1_react extends Component {
  state = {
    province: [{ id: 1, name: "Western province" }],

    district: [
      { id: 1, name: "Kaluthara" },
      { id: 2, name: "Colombo" },
      { id: 3, name: "Gampaha" },
    ],
  };

  render() {
    return (
      <IonGrid>
        {/* 1 row */}
        <IonRow>
          <IonCol>
            <LocationComp
              data={this.state.province}
              type="Province"
              name="name"
              id="id"
            />
          </IonCol>
        </IonRow>

        {/* 2 row */}
        <IonRow>
          <IonCol>
            <LocationComp
              data={this.state.district}
              type="District"
              name="name"
              id="id"
            />
          </IonCol>
        </IonRow>

        {/* 3 row */}
        <IonRow>
          <IonCol>
            <LocationComp
              data={[{ id: 1, name: "Not Yet" }]}
              type="Town"
              name="name"
              id="id"
            />
          </IonCol>
        </IonRow>

        {/* 4 row */}
        <IonRow>
          <IonCol>
            <IonButton fill="outline" expand="block" color="primary">
              Search
            </IonButton>
          </IonCol>
        </IonRow>

      </IonGrid>
    );
  }
}

export default Tab1_react;

我做了一个 react 组件调用 locationComp

import React, { Component } from "react";
import {
  IonButton,
  IonSelect,
  IonSelectOption,
  IonItem,
  IonLabel,
} from "@ionic/react";

class LocationComp extends Component {
  state = {};


  render() {

    const {data,name,id,type} = this.props;

    return (
      <IonItem>
        <IonLabel>{type}</IonLabel>
        <IonSelect
          //value={gender}
          placeholder="Select One"
          //   onIonChange={(e) => setGender(e.detail.value)}
        >
            {data.map(e=>(
                <IonSelectOption key={e["id"]} value="female">{e["name"]}</IonSelectOption>
            ))}

        </IonSelect>
      </IonItem>
    );
  }
}

export default LocationComp;

【问题讨论】:

  • 等等.. 你在使用 TypeScript 吗?还是您项目中的 JavaScript?
  • 我只想使用 jsx 语法。我问有没有可能

标签: reactjs typescript ionic-framework ionic-native ionic-react


【解决方案1】:

由于 ionic 默认使用 TypeScript,如果您希望坚持使用纯 JavaScript,有几个选项可供您选择:

1) 无需将所有.tsx/.tx 文件分别重命名为.jsx/.js -

在您的tsconfig.json 上,您只需将strict 选项设置为false,这实际上将禁用类型检查。

"strict": false 

2) 明确告诉 TypeScript 应该允许 JavaScript 文件 -

一个看似简单的选项是在您的tsconfig.json 上将allowJs 设置为true

"allowJs": true

根据您的项目,您可以选择使用上述选项中的一个或两个。归根结底,TypeScript 是 JavaScript 的超集,只要您禁用伴随使用 TypeScript 的附加功能,常规 JavaScript 就应该在 TypeScript 文件上运行。

【讨论】:

  • 是的,我已经更改了扩展名。但仍然怀疑状态,组件确实挂载方法是否有效。
  • 嗯..我没有使用离子,但我使用了大量的反应/打字稿。根据我的观察,它应该有效。你测试了吗?
猜你喜欢
  • 2016-03-17
  • 2017-06-21
  • 1970-01-01
  • 2012-07-16
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 2021-03-11
  • 2020-12-22
相关资源
最近更新 更多