我正在为我的应用程序使用react typescript.对于样式,我使用样式组件.我决定创建我的自定义钩子输入和密码输入.所以我首先创建了输入包装器,在其中添加了必要的输入字段.之后,我创建了密码输入字段.我用输入组件props 扩展了密码输入组件props .但是当我在密码组件中使用输入组件包装时.我得到了错误:This JSX tag's 'children' prop expects a single child of type 'Element | undefined', but multiple children were provided.

我不知道我做错了什么.

This is my Input wrapper component

import React from "react";
import styled from "styled-components";
import ErrorBoundary from "components/errorBoundary";

const Container = styled.div`
  position: relative;
`;

const Label = styled.label<{ minimized?: boolean }>`
  display: block;
  color: ${({ theme }) => theme.inputLabelColor};
  font-weight: 400;
  font-size: ${({ minimized }) => (minimized ? "11px" : "16px")};
  margin-bottom: 5px;
  letter-spacing: -0.2px;
  position: absolute;
  transform: translateY(${({ minimized }) => (minimized ? "9px" : "16px")});
  left: 20px;
  top: 0;
  pointer-events: none;
  transition: transform 150ms linear, font-size 150ms linear;
`;

const Error = styled.p`
  color: ${({ theme }) => theme.errorTextColor};
`;

const Description = styled.p`
  color: blue;
`;

export interface IInputWrapperProps {
  label?: string;
  required?: boolean;
  minimizedLabel?: boolean;
  description?: string;
  error?: string;
  wrapperStyle?: React.CSSProperties;
  children?: JSX.Element;
}

export default ({
  label,
  error,
  description,
  children,
  required,
  wrapperStyle,
  minimizedLabel
}: IInputWrapperProps) => {
  return (
    <ErrorBoundary id="InputWrapperErrorBoundary">
      <div style={wrapperStyle}>
        <Container>
          <Label minimized={minimizedLabel}>
            {label} {required && <span style={{ color: "red" }}> *</span>}
          </Label>
          {children}
        </Container>
        {description && <Description>{description}</Description>}
        {error && <Error>{error}</Error>}
      </div>
    </ErrorBoundary>
  );
};

This is my password component

import React, { useState } from "react";
import styled from "styled-components";
import eyeHide from "./eye-svgfiles/eyeHide.svg";
import eyeShow from "./eye-svgfiles/eyeShow.svg";
import InputWrapper, { IInputWrapperProps } from "../wrapper";
const Passwordinput = styled.input`
  border: 2px solid ${({ theme }) => theme.inputBorderColorFaded};
  background: ${({ theme }) => theme.inputBackgroundColor};
  display: block;
  border-radius: 5px;
  box-shadow: none;
  color: ${({ theme }) => theme.inputTextColor};
  height: 52px;
  width: 100%;
  margin: 0;
  padding: 1px 15px;
  &:focus {
    border: 2px solid ${({ theme }) => theme.inputBorderColor};
    outline: 0;
  }
`;
const Svg = styled.div`
  position: absolute;
  left: 50%;
  top: 65%;
  transform: scale(0.8);
`;

export interface IPasswordProps extends IInputWrapperProps {
  onChange: (i: string) => void;
  value?: string | undefined;
}
export default ({ onChange, value, label, error, ...rest }: IPasswordProps) => {
  const [state, setstate] = useState(false);
  return (
    <div>
      <InputWrapper label={label} error={error} {...rest}> //I am getting error in here 
        <Passwordinput
          label={label}
          type={state ? "text" : "password"}
          onChange={e => onChange(e.target.value)}
          value={value}
          error={error}
        />
        <Svg>
          <img
            onClick={() => setstate(state => !state)}
            style={{ cursor: "pointer" }}
            src={state ? eyeShow : eyeHide}
            alt="searchIcon"
          />
        </Svg>
      </InputWrapper>
    </div>
  );
};

推荐答案

编写如下界面:

export interface IInputWrapperProps {
  label?: string;
  required?: boolean;
  minimizedLabel?: boolean;
  description?: string;
  error?: string;
  wrapperStyle?: React.CSSProperties;
  children?: JSX.Element|JSX.Element[];
}

事实上:

children: JSX.Element|JSX.Element[];

子元素可以是单个JSX元素,也可以是元素array.

Typescript相关问答推荐

Typescript如何从字符串中提取多个文本

Typescript对每个属性具有约束的通用类型

有没有办法适当缩小类型?

带有微前端的动态React路由问题

带有联合参数的静态大小数组

当使用`type B = A`时,B的类型显示为A.为什么当`type B = A时显示为`any `|用A代替?

类型脚本类型字段组合

TypeScrip:基于参数的条件返回类型

来自类型脚本中可选对象的关联联合类型

TS2339:类型{}上不存在属性消息

在数据加载后,如何使用NgFor在使用异步管道的可观测对象上生成子组件?

Angular 17子路由

如何将v-for中的迭代器编写为v-if中的字符串?

有没有办法取消分发元组的联合?

为什么&Quot;元素隐式具有';Any';类型,因为...即使我已经打字了,也不能作为索引显示错误吗?

是否可以通过映射类型将函数参数约束为预定义类型?

Angular 16将独立组件作为对话框加载,而不进行布线或预加载

任何导航器都未处理有效负载为";params";:{";roomId";:";...";}}的导航操作

如何实现允许扩展泛型函数参数的类型

为什么TS条件返回要求不明确的强制转换而不是精确的强制转换?