React-Typesciprt:此JSX标签的'children'属性期望一个'Element | 未定义”,但提供了多个孩子

克里斯纳

我正在为我的应用程序使用react-typescript。对于样式,我使用样式组件。我已经决定要创建自定义的“挂钩输入”和“密码”输入。因此,我首先创建了输入包装器,在其中添加了必要的输入字段。之后,我创建了密码输入字段。我用输入组件属性扩展了我的密码输入组件属性。但是当我使用Input组件包装到我的密码组件时。我收到错误消息:This JSX tag's 'children' prop expects a single child of type 'Element | undefined', but multiple children were provided.

我不知道我在做什么错。

这是我的输入包装器组件

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>
  );
};

这是我的密码部分

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>
  );
};
蹲SlavIn运动服

就像错误所说的一样,您只能提供1个元素作为子元素。您可以将它们包装成一个片段,如下所示:

InputWrapper label={label} error={error} {...rest}>
<>
        <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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

React.Children.only 期望接收单个 React 元素孩子。?

来自分类Dev

React Native 应用程序显示一个错误,指出即使提供了密钥,密钥也将是未定义的

来自分类Dev

React错误'Failed propType:提供给`Provider`的无效prop'children',应该是一个ReactElement

来自分类Dev

调试带有Chrome扩展“ React Developer Tools 4.10.1”的React App我期望看到一个React标签

来自分类Dev

尝试使用react-leaflet-choropleth映射一个Choropleth时出错:无法读取未定义的属性“ map”

来自分类Dev

jQuery最后一个孩子的未定义问题

来自分类Dev

jQuery 最后一个孩子 val() 未定义

来自分类Dev

未定义用于创建自定义 HTML 标签的 React 属性

来自分类Dev

当使用this.props.children渲染子代时,React上下文未定义

来自分类Dev

使用React Router和Webpack获取异步组件时未定义this.props.children

来自分类Dev

react-native-Picker-未定义不是对象(评估'this.props.children [position] .props)

来自分类Dev

[PHP] [XML] SimpleXML children()仅返回一个孩子

来自分类Dev

React中的Typescript:在`this`上定义一个属性

来自分类Dev

.children(“:first-child”)-ajax-多个第一个孩子?

来自分类Dev

根据另一个JSX属性react设置JSX属性

来自分类Dev

React:期望赋值或函数调用,而是在 JSX 中看到了一个表达式

来自分类Dev

React无法读取未定义的属性

来自分类Dev

React JS,尝试从数组中获取第一个值时未定义错误未定义

来自分类Dev

在另一个函数React返回的匿名函数中,“ this”是未定义的

来自分类Dev

React Native:另一个“未定义不是对象(评估 action.type)

来自分类Dev

ESLint:未定义“头盔” react / jsx-no-undef

来自分类Dev

未定义'组件'react / jsx-no-undef

来自分类Dev

期望深层属性具有多个值中的任何一个

来自分类Dev

放心。期望数组元素定义了一个以上的属性值

来自分类Dev

将此this.props.children传递到react-router中的另一个文件

来自分类Dev

如何在React中的React.createElement()内部定义一个img标签?

来自分类Dev

反应this.props.children是未定义的

来自分类Dev

未定义的值.children()。each()

来自分类Dev

多个find_by属性为nil:NilClass错误提供了未定义的方法

Related 相关文章

  1. 1

    React.Children.only 期望接收单个 React 元素孩子。?

  2. 2

    React Native 应用程序显示一个错误,指出即使提供了密钥,密钥也将是未定义的

  3. 3

    React错误'Failed propType:提供给`Provider`的无效prop'children',应该是一个ReactElement

  4. 4

    调试带有Chrome扩展“ React Developer Tools 4.10.1”的React App我期望看到一个React标签

  5. 5

    尝试使用react-leaflet-choropleth映射一个Choropleth时出错:无法读取未定义的属性“ map”

  6. 6

    jQuery最后一个孩子的未定义问题

  7. 7

    jQuery 最后一个孩子 val() 未定义

  8. 8

    未定义用于创建自定义 HTML 标签的 React 属性

  9. 9

    当使用this.props.children渲染子代时,React上下文未定义

  10. 10

    使用React Router和Webpack获取异步组件时未定义this.props.children

  11. 11

    react-native-Picker-未定义不是对象(评估'this.props.children [position] .props)

  12. 12

    [PHP] [XML] SimpleXML children()仅返回一个孩子

  13. 13

    React中的Typescript:在`this`上定义一个属性

  14. 14

    .children(“:first-child”)-ajax-多个第一个孩子?

  15. 15

    根据另一个JSX属性react设置JSX属性

  16. 16

    React:期望赋值或函数调用,而是在 JSX 中看到了一个表达式

  17. 17

    React无法读取未定义的属性

  18. 18

    React JS,尝试从数组中获取第一个值时未定义错误未定义

  19. 19

    在另一个函数React返回的匿名函数中,“ this”是未定义的

  20. 20

    React Native:另一个“未定义不是对象(评估 action.type)

  21. 21

    ESLint:未定义“头盔” react / jsx-no-undef

  22. 22

    未定义'组件'react / jsx-no-undef

  23. 23

    期望深层属性具有多个值中的任何一个

  24. 24

    放心。期望数组元素定义了一个以上的属性值

  25. 25

    将此this.props.children传递到react-router中的另一个文件

  26. 26

    如何在React中的React.createElement()内部定义一个img标签?

  27. 27

    反应this.props.children是未定义的

  28. 28

    未定义的值.children()。each()

  29. 29

    多个find_by属性为nil:NilClass错误提供了未定义的方法

热门标签

归档