warning: React does not recognize the xxx prop on a DOM element
歡迎使用Markdown編輯器
1、錯誤提示
Warning: React does not recognize the disableValue
prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase disablevalue
instead. If you accidentally passed it from a parent component, remove it from the DOM element
2、分析原因
這是React不能識別dom元素上的非標(biāo)準(zhǔn)attribute報出的警告,最終的渲染結(jié)果中React會移除這些非標(biāo)準(zhǔn)的attribute。文章來源:http://www.zghlxwxcb.cn/news/detail-763756.html
const MyCheckboxGroup: React.FC<MyCheckboxGroupProps> = props => {
const { data, value, onChange, style, disableValue } = props;
useEffect(()=>{
onChange?.(value);
}, [value])
return (
<Checkbox.Group
{...props}
value={value}
onChange={ onChange}
style={style || {}}
>
{Object.getOwnPropertyNames(data).map((item: any, index: any) => (
<Checkbox value={Number.parseInt(item, 10).toString()} style={{ marginLeft: 0 }} disabled={disableValue?.includes(item)} key={index}>
{
typeof data[item] === 'string'
? data[item]
: data[item]?.text
}
</Checkbox>
))}
</Checkbox.Group>
);
};
3、解決
可以使用other接收屬性參數(shù),僅將other屬性參數(shù)傳遞給子組件或?qū)?yīng)的dom,自定義屬性只組件自己使用。文章來源地址http://www.zghlxwxcb.cn/news/detail-763756.html
const MyCheckboxGroup: React.FC<MyCheckboxGroupProps> = props => {
const { data, value, onChange, style, disableValue, ...other } = props;
useEffect(()=>{
onChange?.(value);
}, [value])
return (
<Checkbox.Group
{...other}
value={value}
onChange={ onChange}
style={style || {}}
>
{Object.getOwnPropertyNames(data).map((item: any, index: any) => (
<Checkbox value={Number.parseInt(item, 10).toString()} style={{ marginLeft: 0 }} disabled={disableValue?.includes(item)} key={index}>
{
typeof data[item] === 'string'
? data[item]
: data[item]?.text
}
</Checkbox>
))}
</Checkbox.Group>
);
到了這里,關(guān)于warning: React does not recognize the xxx prop on a DOM element的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!