admin管理员组文章数量:1313614
I have a simple page with input fields and a button and I want to make button disabled when both field length is less than 3. It is simple thing but I am confused about the hook because it is going into infinite loop. Here is code:
function TableFooterPanel(props) {
const [firstName, setFirstName] = useState('');
const [lastName, setLastName] = useState('');
// const [isButtonDisabled, setIsButtonDisabled] = useState(true);
const addNewCustomer = async (name, surname) => {
await service.addCustomer(name, surname);
props.funcParam();
}
var isButtonDisabled = false;
if (firstName <= 3 || lastName <= 3) {
isButtonDisabled = true;
}
else {
isButtonDisabled = false;
}
return (
<>
<Card className='buttonFooter'>
<Form className='buttonFooter'>
<input type="text" placeholder="First Name" defaultValue={firstName} onChange={e => setFirstName(e.target.value)}></input>
<input type="text" placeholder="Last Name" defaultValue={lastName} onChange={e => setLastName(e.target.value)}></input>
<Button disabled={isButtonDisabled} onClick={() => addNewCustomer(firstName, lastName)}>Add</Button>
</Form>
</Card>
</>
);
}
export default TableFooterPanel;
With this code, the boxes only check whether they are empty or filled.
If I unment useState hook and try to set it in if-else condition then this time infinite-loop is happening and page is crashing.
How can I check the input length without falling into infinite-loop?
I have a simple page with input fields and a button and I want to make button disabled when both field length is less than 3. It is simple thing but I am confused about the hook because it is going into infinite loop. Here is code:
function TableFooterPanel(props) {
const [firstName, setFirstName] = useState('');
const [lastName, setLastName] = useState('');
// const [isButtonDisabled, setIsButtonDisabled] = useState(true);
const addNewCustomer = async (name, surname) => {
await service.addCustomer(name, surname);
props.funcParam();
}
var isButtonDisabled = false;
if (firstName <= 3 || lastName <= 3) {
isButtonDisabled = true;
}
else {
isButtonDisabled = false;
}
return (
<>
<Card className='buttonFooter'>
<Form className='buttonFooter'>
<input type="text" placeholder="First Name" defaultValue={firstName} onChange={e => setFirstName(e.target.value)}></input>
<input type="text" placeholder="Last Name" defaultValue={lastName} onChange={e => setLastName(e.target.value)}></input>
<Button disabled={isButtonDisabled} onClick={() => addNewCustomer(firstName, lastName)}>Add</Button>
</Form>
</Card>
</>
);
}
export default TableFooterPanel;
With this code, the boxes only check whether they are empty or filled.
If I unment useState hook and try to set it in if-else condition then this time infinite-loop is happening and page is crashing.
How can I check the input length without falling into infinite-loop?
Share Improve this question edited Apr 14, 2022 at 12:42 abidinberkay asked Apr 14, 2022 at 12:37 abidinberkayabidinberkay 2,0355 gold badges49 silver badges78 bronze badges 1- Off topic, I know, but setting the limit to 3 characters for a name is extreme. Amy, Abu, Ash, Abe, Sal, Nic, Leo, and Ian aren't going to be happy. – smassey Commented Apr 14, 2022 at 12:50
6 Answers
Reset to default 4Try :
if (firstName.length <= 3 || lastName.length <= 3) {
isButtonDisabled = true;
}
else {
isButtonDisabled = false;
}
I suggest you to not write code directly in ponent's body: you don't know how many times that code will be exected. Much better use a useEffect
hook:
function TableFooterPanel(props) {
const [firstName, setFirstName] = useState('');
const [lastName, setLastName] = useState('');
const [isButtonDisabled, setIsButtonDisabled] = useState(true);
const addNewCustomer = async (name, surname) => {
await service.addCustomer(name, surname);
props.funcParam();
}
useEffect(() => {
if (firstName.length <= 3 || lastName.length <= 3) {
setIsButtonDisabled(true);
}
else {
setIsButtonDisabled(false);
}
}, [firstName, lastName]);
return (
<>
<Card className='buttonFooter'>
<Form className='buttonFooter'>
<input type="text" placeholder="First Name" defaultValue={firstName} onChange={e => setFirstName(e.target.value)}></input>
<input type="text" placeholder="Last Name" defaultValue={lastName} onChange={e => setLastName(e.target.value)}></input>
<Button disabled={isButtonDisabled} onClick={() => addNewCustomer(firstName, lastName)}>Add</Button>
</Form>
</Card>
</>
);
}
export default TableFooterPanel;
As you can see, I have used an useEffect
hook with firstName
and lastName
in deps list: this means that, every time firstName
or lastName
change, useEffect
will be fired.
And don't forget that, to check string length you have to use length
property =).
Here you go: Codesandbox demo
You could simple add the disabled argument inline like this:
<button
disabled={firstName.length < 3 || lastName.length < 3}
onClick={() => addNewCustomer(firstName, lastName)}
>
Add
</button>
function TableFooterPanel(props) {
const [firstName, setFirstName] = useState('');
const [lastName, setLastName] = useState('');
const addNewCustomer = async (name, surname) => {
await service.addCustomer(name, surname);
props.funcParam();
}
return (
<>
<Card className='buttonFooter'>
<Form className='buttonFooter'>
<input type="text" placeholder="First Name" defaultValue={firstName} onChange={e => setFirstName(e.target.value)}></input>
<input type="text" placeholder="Last Name" defaultValue={lastName} onChange={e => setLastName(e.target.value)}></input>
<Button disabled={!(firstName.length < 3 || lastName.length < 3)} onClick={() => addNewCustomer(firstName, lastName)}>Add</Button>
</Form>
</Card>
</>
);
}
export default TableFooterPanel;
Less than 3 is < 3
, not <= 3
There's no need for an if condition here, just set the value of isButtonDisabled directly:
const isButtonDisabled = firstName.lenght < 3 || lastName.lenght < 3;
Or just do it inline:
<Button disabled={firstName.lenght < 3 || lastName.lenght < 3} />
You need to put them inside a useEffect
otherwise it will re-run when anything on this page changes and make the isButtonDisabled
variable as a state.
const [isButtonDisabled, setIsButtonDisabled] = useState(true)
useEffect(()=>{
if (firstName.length <= 3 || lastName.length <= 3) {
setIsButtonDisabled(true);
} else {
setIsButtonDisabled(false);
}
},[firstName, lastName])
本文标签: javascriptHow to disable button according to input length by using React useStateStack Overflow
版权声明:本文标题:javascript - How to disable button according to input length by using React useState? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741949885a2406648.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论