admin管理员组文章数量:1187302
I've created a custom button component based on Material-UI's Button component, so I can overlay a loading circle whenever an action is pending. However, React complains with the following:
Warning: Received false
for a non-boolean attribute loading
.
If you want to write it to the DOM, pass a string instead: loading="false" or loading={value.toString()}.
Here's what my component looks like. Thanks in advance!
import * as React from 'react'
import { ButtonProps } from '@material-ui/core/Button'
import { Button, CircularProgress } from '@material-ui/core'
interface IProps extends ButtonProps {
loading: boolean
}
export const LoadingButton = (props: IProps) => {
const { disabled, loading } = props
return (
<div className='button-container'>
<Button {...props} disabled={disabled == true || loading == true}/>
{loading == true && (
<CircularProgress size={24} className='button-progress' />
)}
</div>
)
}
I've created a custom button component based on Material-UI's Button component, so I can overlay a loading circle whenever an action is pending. However, React complains with the following:
Warning: Received false
for a non-boolean attribute loading
.
If you want to write it to the DOM, pass a string instead: loading="false" or loading={value.toString()}.
Here's what my component looks like. Thanks in advance!
import * as React from 'react'
import { ButtonProps } from '@material-ui/core/Button'
import { Button, CircularProgress } from '@material-ui/core'
interface IProps extends ButtonProps {
loading: boolean
}
export const LoadingButton = (props: IProps) => {
const { disabled, loading } = props
return (
<div className='button-container'>
<Button {...props} disabled={disabled == true || loading == true}/>
{loading == true && (
<CircularProgress size={24} className='button-progress' />
)}
</div>
)
}
Share
Improve this question
edited Aug 8, 2019 at 6:56
frogatto
29.3k13 gold badges89 silver badges134 bronze badges
asked Aug 8, 2019 at 5:44
cameraguy258cameraguy258
6793 gold badges10 silver badges21 bronze badges
4
|
2 Answers
Reset to default 27You shouldn't pass your custom props down to the Button.
Replace
const { disabled, loading } = props
with
const { disabled, loading, ...rest } = props
and then
<Button {...rest} disabled={disabled || loading}/>
Your problem is that you are passing all your props to the Button
component. Remove {...props}
from <Button />
and declare the attributes separately.
Looking at the documentation, you can see that loading
it is not an attribute on the Button
component, but the spread operator ...
still add it.
<div className="button-container">
<Button disabled={disabled === true || loading === true} />
{loading === true && <CircularProgress size={24} className="button-progress" />}
</div>
)
Also, as a side note, I recommend that you use the ===
identity operator instead of the ==
equality operator. See this post Which equals operator (== vs ===) should be used in JavaScript comparisons?
本文标签: javascriptReceived false for a nonboolean attribute loadingStack Overflow
版权声明:本文标题:javascript - Received `false` for a non-boolean attribute `loading` - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738337046a2077114.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
<LoadingButton disabled={false} loading="false">My Button</LoadingButton>
– It'sNotMe Commented Aug 8, 2019 at 6:33