reactjs - Uncaught (in promise) TypeError: mostrarError 不是一个函数。带挂钩
问题描述
函数“mostrarError”来自道具,我将它放入函数“handleImagenSeleccionada”,但是当它在“catch(error)”中使用时,它显示下一个错误:Uncaught (in promise) TypeError: mostrarError is不是函数。
export default function LoadImage({ mostrarError }) {
const [imagenURL, setImagenURL] = useState('');//URL gotten from the backend when the image was loaded in the server
const [subiendoImagen, setSubiendoImagen] = useState(false);// For the loading
//------------------------ Functions ---------------------------------
async function handleImagenSeleccionada(evento) {
try {
setSubiendoImagen(true);
const file = evento.target.files[0];
const formData = new FormData();
formData.append('image', file);
const { data } = await Axios.post(baseURL + '/inside/postImage', formData, { headers: { "Content-type": "multipart/form-data" } });
setImagenURL(data.url);
setSubiendoImagen(false);
} catch (error) {
setSubiendoImagen(false);
console.log(mostrarError);
mostrarError(error.response.data.message);
}
}
return (
//Form in JSX
);
}
我使用console.log 来显示“mostrarError”,而cosoles 告诉我“mostrarError”是一个函数。 在此处输入图像描述
解决方案
您的控制台日志输出显示mostrarError
为一个包含名为 的函数的对象mostrarError
,即:
{
mostrarError: function () {...}
}
在调用它时使用mostrarError.mostrarError
,或者(可能是更好的解决方案)确保将函数传递给mostrarError
道具,
推荐阅读
- sql-server - SQL Server:输出选择表?
- python - 通过 Google 广告 API 获取出价模拟器结果
- c++ - glClearColor 被忽略
- php - 使用 Laravel 在目录中查找最小文件大小?
- java - 多对多关系springboot mysql
- r - 如何计算不同区域大小的斜率
- javascript - 新创建的快速路线在网站上返回 CANNOT GET /。在本地主机上工作正常
- c# - 单击 CheckedListBox 中的项目对最后单击的项目有影响
- php - 如何正确使用 WC_Order 类?
- eloquent - Eloquent:如何从集合中获取与指定集合中所有项目相关的值?