簡體   English   中英

如何為文本區域鍵入道具反應打字稿

[英]how to type props for text area react typescript

我有一個看起來像這樣的反應組件:

import { TextareaHTMLAttributes} from 'react'
import styled from 'styled-components'

const TextAreaElement = styled.textarea`
  border-radius: 40px;
  border: none;
  background: white;
`

const TextArea = (props: TextareaHTMLAttributes<any>) => {  <--- replace <any> here
  return <TextAreaElement {...props} />
}

我知道我可以做這樣的事情,但不想手動添加每個道具:

const TextArea = ({placeholder} : {placeholder: string}) => {
  return <TextAreaElement placeholder={placeholder} />
}

您可以將道具作為常規 HTML 元素傳遞

import React from "react";

const CustomTA = (props: React.HTMLProps<HTMLTextAreaElement>) => {
  return <textarea {...props} />;
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM