typescript +SolidJS : how can I extend props of some JSX element?

标签 typescript solid-js

如何扩展一些现有 JSX 元素的 SolidJS props 并创建我的自定义界面,如下面给出的 React 示例中的 ButtonProps 界面。

import React from 'react';

interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
  title: string;
  showIcon: boolean;
}

const Button: React.FC<ButtonProps> = ({ title, showIcon, ...props }) => {
  return (
    <button {...props}>
      {title}
      {showIcon && <Icon/>}
    </button>
  );
};

最佳答案

以非常相同的方式,除了您需要避免对 react 性属性进行解构并改用 splitProps,因此您的示例将转换为:

import { splitProps, JSX } from 'solid-js';

interface ButtonProps extends JSX.ButtonHTMLAttributes<HTMLButtonElement> {
  title: string;
  // better make that optional, so you can use Button without having to
  // add the showIcon attribute:
  showIcon?: boolean;
}

const Button = (props: ButtonProps) => {
  const [local, buttonProps] = splitProps(props, ['title', 'showIcon']);
  return (
    <button {...buttonProps}>
      {local.title}
      {local.showIcon && <Icon/>}
    </button>
  );
};

如您所见,内在元素属性的类型在 JSX 中,来自将 JSX 转换为实体响应式(Reactive)表达式的 jsx-dom-expressions。

关于 typescript +SolidJS : how can I extend props of some JSX element?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72126059/

相关文章:

javascript - 出于相同目的在两个地方使用路由器指令

angular - 从 ngrx/store 获取单个项目

solid-js - 在 SolidJS 中,如何做后备路由?

javascript - 如何在 Angular 6 中注入(inject)/集成第三​​方库?

javascript - typescript :在类型 'string' A 上找不到参数类型为 '{ "的索引签名“:字符串;}

typescript - 允许从 ECS 连接到现有的 RDS 数据库?

typescript - 使用 SolidJS 中的 fetch API 渲染远程数据

javascript - SolidJS - 如何使用外部依赖触发 createEffect?

solid-js - Solid JS 是否有 React.useCallback 的等价物?