问题描述
Getting data in the console but want to import the var image to be used in another component
Example would be to do this in another component
,然后将图像var发送为wel 问题是我的组件如何即时返回数据,还是问题我的组件如何即时返回数据,还是问题我的组件如何即时返回数据,还是 //////////////////////////////////////////////////// //////////////////////////////////////////////// 从'react'导入React,{useState}; 从“ axios”导入axios;
export default function LogoUpload() {
const [image,setImage] = useState(''); //Wnat to export this variable
const [selectedFile,setSelectedFile] = useState('');
const fileUpload = React.createRef();
function onFileChangeHandler(e) {
// routine to run when the user selects a file to upload as an image
e.preventDefault();
const file = e.target.files[0];
var promise = new Promise((resolve,reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result);
reader.onerror = e => reject(e);
});
promise.then(response => {
setImage(response);
setSelectedFile(file);
console.log(response);
});
}
function showFileUpload(e) {
e.preventDefault();
fileUpload.current.click();
return image;
}
// export this function dont know how :(
function getImageData() {
return image;
}
return (
//Click and display image and get data
<div className="AddImage">
<input
type="file"
id="imageUpload"
style={{ display: 'none' }}
ref={fileUpload}
onChange={onFileChangeHandler}
/>
<input
type="image"
style={{
width: '100px',height: '100px',borderRadius: '50%',objectFit: 'cover',objectPosition: 'center',}}
src={image}
alt="Add Logo"
onClick={showFileUpload}
/>
</div>
);
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)