reactjs - 不能将函数作为道具传递,总是有警告?
问题描述
这是我的代码:
const handleNotification = React.useCallback(
type => {
setSuccess(type);
setTimeout(() => reset(), 1500);
},
[reset]
);
const sendRequest = React.useCallback(
async () => {
// don't send again while we are sending
if (isSending) return;
// update state
setIsSending(true);
// send the actual request
try {
await axios({
method: "post",
url: NONCOM_SEND_MAIL_URL,
headers: {},
data: {
clientId,
userId,
email,
subject: "Non-Compliant Certificate",
templateCode: "REQNOTMET",
entityId: entity_id
}
});
handleNotification(true);
} catch (e) {
setError(e);
handleNotification(false);
}
// once the request is sent, update state again
if (isMounted.current)
// only update if we are still mounted
setIsSending(false);
},
[isSending, clientId, userId, email, entity_id, handleNotification]
); // update the callback if the state changes
我收到此警告:
'reset' 函数使 useCallback Hook(在第 35 行)的依赖关系在每次渲染时都发生变化。将其移动到 useCallback 回调中。或者,将“重置”定义包装到它自己的 useCallback() 挂钩中
如您所见,我已经将它包装在 useCallback 中?在这种情况下我做错了什么?reset 是此组件中的功能道具。为什么这不起作用?
解决方案
推荐阅读
- c# - C# - webclient 解码 URL 编码字符串上的括号
- sql - 在日期时间数据类型的 where 子句中使用日期值
- python - 在 Dataframe float32 列上使用 list(zip(...)) 时出现浮动问题
- apache - 如何在 macOS Mojave 中为 Apache 配置“AuthType Kerberos”?
- laravel - 使用 DomPDF 生成 PDF 时如何在刀片上显示新行?
- apache - 导致错误 400 的翻译端口:原因:您正在对启用 SSL 的服务器端口使用纯 HTTP
- c# - 如何从 UWP 应用拖放拖放后创建的虚拟文件?
- reactjs - 在 React 应用程序中分离 HTMLDivElements 的原因
- c# - LINQ 中的计数(不同 x)
- c# - 用户控制 Page_Load 运行两次