Reactjs 未捕获(承诺中)类型错误:mostrarError不是函数。用钩子
函数“mostrarError”来自props,我把它放在函数“handleimageneselectionada”中,但当它用于“catch(error)”时,它会显示下一个错误:Uncaught(in promise)TypeError: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
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,谢谢。你的第一个选择就是答案。