[英]React with a handler function that returns a function (a curried) - what's the benefit of using it?
I'm writing a component that renders Checkboxes
of Categories
as part of a large system, here is the code:我正在编写一个组件,将
Categories
Checkboxes
呈现为大型系统的一部分,这是代码:
import React, { useState, useEffect } from 'react';
const Checkbox = ({ categories, handleFilter }) => {
const [checked, setChecked] = useState([]);
// get all the categories that user clicked and send them to the Backend
const handleToggle = c => () => {
const currentCategoryId = checked.indexOf(c); // return the first index with cateogry 'c
const newCheckedCategoryId = [...checked];
// if currently checked wasn't already in Checked state ==> Push
// else Pull it
// User want to Check
if (currentCategoryId === -1) {
// not in the state
newCheckedCategoryId.push(c);
}
else {
// User wants to Uncheck
newCheckedCategoryId.splice(currentCategoryId, 1); // remove it from the array
}
// console.log(newCheckedCategoryId);
setChecked(newCheckedCategoryId);
handleFilter(newCheckedCategoryId, 'category');
}
return (
<>
{
categories.map((cat, index) => (
<li key={index} className='list-unstyled'>
<input
onChange={handleToggle(cat._id)}
value={checked.indexOf(cat._id) === -1}
type='checkbox'
className='form-check-input' />
<label className='form-check-label'>{cat.name}</label>
</li>
))
}
</>
)
};
export default Checkbox;
Whenever a user clicks one of the Checkboxes
the handler handleToggle
is invoked.每当用户单击其中一个
Checkboxes
,就会调用处理程序handleToggle
。
What I don't understand is why I need to use a curried function?我不明白的是为什么我需要使用柯里化函数?
When I try to use:当我尝试使用:
const handleToggle = c => { ... }
Instead of:代替:
const handleToggle = c => () => { ... }
React throws:反应抛出:
Unhandled Rejection (Error): Too many re-renders. React limits the number of renders to prevent an infinite loop.
What's the deal here and why is it required to use a curried function ?这里有什么交易,为什么需要使用柯里化函数?
The main problem is handleToggle
has been evaluated and assigned the result of that function to onChange
instead of passing as a callback function.主要问题是
handleToggle
已被评估并将该函数的结果分配给onChange
而不是作为回调函数传递。
Based on that you need to change how you call the function handleToggle
as:基于此,您需要将调用函数
handleToggle
方式更改为:
onChange={() => handleToggle(cat._id)}
From your example:从你的例子:
onChange={handleToggle(cat._id)}
And at the end const handleToggle = c => { ... }
will be just fine.最后
const handleToggle = c => { ... }
会很好。
According to your onChange
function you are calling function directly instead of assigning it:根据您的
onChange
函数,您直接调用函数而不是分配它:
onChange={handleToggle(cat._id)} <- Call as soon as this sees
instead you have to do this:相反,你必须这样做:
onChange={() => handleToggle(cat._id)}
And define handleToggle
like this:并像这样定义
handleToggle
:
// get all the categories that user clicked and send them to the Backend
const handleToggle = c => {
const currentCategoryId = checked.indexOf(c); // return the first index with cateogry 'c
const newCheckedCategoryId = [...checked];
// if currently checked wasn't already in Checked state ==> Push
// else Pull it
// User want to Check
if (currentCategoryId === -1) {
// not in the state
newCheckedCategoryId.push(c);
}
else {
// User wants to Uncheck
newCheckedCategoryId.splice(currentCategoryId, 1); // remove it from the array
}
// console.log(newCheckedCategoryId);
setChecked(newCheckedCategoryId);
handleFilter(newCheckedCategoryId, 'category');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.