未捕获(承诺)TypeError:mostrarError不是函数。带钩

时间:2020-09-12 19:39:11

标签: reactjs react-hooks

函数“ 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”是一个函数。 enter image description here

1 个答案:

答案 0 :(得分:0)

您的控制台日志输出显示mostrarError是对象包含名为mostrarError的函数,即:

{
  mostrarError: function () {...}
}

在调用时使用mostrarError.mostrarError,或者(可能是更好的解决方案)确保将函数传递给mostrarError道具,