如何在使用效果中调用功能?

发布于 2025-01-28 15:24:07 字数 2165 浏览 5 评论 0原文

我想在下面提到的函数 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吹泡泡o 2025-02-04 15:24:07

您可以删除很多东西

import { useEffect, useState } from "react";
import Child from "./Child";

const Parent = () => {
  const [childInputValue, setChildInputValue] = useState(null);

  return (
    <div>
      <h1>Parent Component</h1>
      <p>{childInputValue}</p>

      {/* We pass function to the child component. */}
      <Child onSaveInputValue={setChildInputValue} />
    </div>
  );
};

export default Parent;

You can remove a lot of stuff

import { useEffect, useState } from "react";
import Child from "./Child";

const Parent = () => {
  const [childInputValue, setChildInputValue] = useState(null);

  return (
    <div>
      <h1>Parent Component</h1>
      <p>{childInputValue}</p>

      {/* We pass function to the child component. */}
      <Child onSaveInputValue={setChildInputValue} />
    </div>
  );
};

export default Parent;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文