如何在使用效果中调用功能?
我想在下面提到的函数 onsaveInputValue 使用effeffect ,以便我的组件可以通过 值 ChildInputValue hook每当页面加载时。
const onSaveInputValue = (value) => {
setChildInputValue(value);
console.log(childInputValue);
};
我试图这样做,但做不到。请引导我。
如果您想查看组件的完整代码,则如下所述。
父组件: -
import { useEffect, useState } from "react";
import Child from "./Child";
const Parent = () => {
const [childInputValue, setChildInputValue] = useState();
// useEffect(() => {});
const onSaveInputValue = (value) => {
setChildInputValue(value);
console.log("childVInputValue",value)
};
return (
<div>
<h1>Parent Component</h1>
<p>{childInputValue}</p>
{/* We pass function to the child component. */}
<Child onSaveInputValue={onSaveInputValue} />
</div>
);
};
export default Parent;
儿童组件: -
import React, { useEffect, useState } from "react";
import "./App.css";
function Child(props) {
const [baseImage, setBaseImage] = useState("");
const uploadImage = async (e) => {
const file = e.target.files[0];
const base64 = await convertBase64(file);
setBaseImage(base64);
props.onSaveInputValue(baseImage);
};
const convertBase64 = (file) => {
return new Promise((resolve, reject) => {
const fileReader = new FileReader();
fileReader.readAsDataURL(file);
fileReader.onload = () => {
resolve(fileReader.result);
};
fileReader.onerror = (error) => {
reject(error);
};
});
};
useEffect(()=>{
console.log("useeeffect",baseImage)
})
return (
<div >
<input
type="file"
onChange={(e) => {
uploadImage(e);
}}
/>
<br></br>
<img src={baseImage} height="200px" />
<hr/>
</div>
);
}
export default Child;
I want to call below mentioned function onSaveInputValue into the useEffect so that, my component can have passed value in childInputValue hook whenever the page gets loaded.
const onSaveInputValue = (value) => {
setChildInputValue(value);
console.log(childInputValue);
};
I have tried to do so but couldn't do it. Please guide me.
In case if you like to see the full code of components then it is mentioned as below.
Parent Component:-
import { useEffect, useState } from "react";
import Child from "./Child";
const Parent = () => {
const [childInputValue, setChildInputValue] = useState();
// useEffect(() => {});
const onSaveInputValue = (value) => {
setChildInputValue(value);
console.log("childVInputValue",value)
};
return (
<div>
<h1>Parent Component</h1>
<p>{childInputValue}</p>
{/* We pass function to the child component. */}
<Child onSaveInputValue={onSaveInputValue} />
</div>
);
};
export default Parent;
Child Component:-
import React, { useEffect, useState } from "react";
import "./App.css";
function Child(props) {
const [baseImage, setBaseImage] = useState("");
const uploadImage = async (e) => {
const file = e.target.files[0];
const base64 = await convertBase64(file);
setBaseImage(base64);
props.onSaveInputValue(baseImage);
};
const convertBase64 = (file) => {
return new Promise((resolve, reject) => {
const fileReader = new FileReader();
fileReader.readAsDataURL(file);
fileReader.onload = () => {
resolve(fileReader.result);
};
fileReader.onerror = (error) => {
reject(error);
};
});
};
useEffect(()=>{
console.log("useeeffect",baseImage)
})
return (
<div >
<input
type="file"
onChange={(e) => {
uploadImage(e);
}}
/>
<br></br>
<img src={baseImage} height="200px" />
<hr/>
</div>
);
}
export default Child;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以删除很多东西
You can remove a lot of stuff