函数“ mostrarError”来自道具,我将其放入函数“ handleImagenSeleccionada”中,但在“捕获(错误)”中使用时,显示下一个错误:未捕获(承诺)TypeError:mostrarError是不是功能。
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”,而控制台显示了“ mostrarError”是一个函数。在此处输入图片说明
您的控制台日志输出显示mostrarError
是一个包含名为的函数的对象mostrarError
,即:
{
mostrarError: function () {...}
}
mostrarError.mostrarError
调用时使用它,或(可能是更好的解决方案)确保将函数传递给mostrarError
prop,
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句