這是我對 MUI TextField的反應 js 代碼,當錯誤道具有值時,我需要應用錯誤和helperText屬性。默認錯誤道具值為空。
import React from 'react'
import { TextField } from '@mui/material'
const InputField = (props) => {
const { name, label, value,error=null, onChange } = props;
return (
<TextField
variant='outlined'
label={label}
name={name}
value={value}
onChange={onChange}
{...{error && {error:true,helperText:error}}}
/>
)
}
export default InputField
錯誤在這一行。我如何在 MUI 5.10.9 版本上找到它
{...{error && {error:true,helperText:error}}}
uj5u.com熱心網友回復:
無條件直接使用error和道具。當不存在錯誤時,helperText還可以在 helperText 中使用字符以避免高度問題。https://mui.com/material-ui/react-text-field/#helper-textspace
<TextField
variant='outlined'
label={label}
name={name}
value={value}
onChange={onChange}
error={!!error}
helperText={error || ' '}
/>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/514389.html
下一篇:為什么驗證沒有按預期作業?
