Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 未捕获(承诺中)类型错误:mostrarError不是函数。用钩子_Reactjs_React Hooks - Fatal编程技术网

Reactjs 未捕获(承诺中)类型错误:mostrarError不是函数。用钩子

Reactjs 未捕获(承诺中)类型错误:mostrarError不是函数。用钩子,reactjs,react-hooks,Reactjs,React Hooks,函数“mostrarError”来自props,我把它放在函数“handleimageneselectionada”中,但当它用于“catch(error)”时,它会显示下一个错误:Uncaught(in promise)TypeError:mostrarError不是一个函数 export default function LoadImage({ mostrarError }) { const [imagenURL, setImagenURL] = useState('');//URL got

函数“mostrarError”来自props,我把它放在函数“handleimageneselectionada”中,但当它用于“catch(error)”时,它会显示下一个错误:Uncaught(in promise)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”,cosole显示“mostrarError”是一个函数。

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

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

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

谢谢。你的第一个选择就是答案。