asif kaif
asif kaif

Reputation: 159

How to pass data from Child function to parent component in react

I want url in parent component and want to save it in imageUrl state, url is in handleUploadImage function ( image download url is the url which I need in parent component)

myFunc.js

export const handleUploadImage = (image: any, folderName = "adhaar") =>{
    let file = image;
    const storage = firebase.storage();
    const storageRef = storage.ref();
    const uploadTask = storageRef.child(`${folderName}/` + file.name).put(file);
  
    uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,
      (snapshot) =>{
        storageRef.child(file.name).getDownloadURL().then(url => { 
            console.log(url)
        })        
      },(error) =>{
        throw error
      },() => {        
        uploadTask.snapshot.ref.getDownloadURL().then((url) =>{           
          console.log("image download url:", url)
        })  
     }
   ) 
  }

parent Component

const ParentComponent = ()=>
{
    const [imageUrl setImageUrl] =useState()

      const getUrl = ()=>
      {
        handleUploadImage(image)
          setImageUrl()
      }

return(
   <button onClick={getUrl}> Get Url </button>

)


}


Upvotes: 0

Views: 61

Answers (2)

Dawood Ahmad
Dawood Ahmad

Reputation: 474

just pass your setImage to the child and set it there, just like this:

    const ParentComponent = ()=>
{
    const [imageUrl, setImageUrl] =useState()

      const getUrl = ()=>
      {
        handleUploadImage(image, setImage)
          
      }

return(
   <button onClick={getUrl}> Get Url </button>

)


}
/////////////////////

    export const handleUploadImage = (image, setImage) =>
  new Promise((resolve, reject) => {
    let file = image;
    const storage = firebase.storage();
    const storageRef = storage.ref();
    const uploadTask = storageRef.child(`${folderName}/` + file.name).put(file);

    uploadTask.on(
      firebase.storage.TaskEvent.STATE_CHANGED,
      (snapshot) => {
        storageRef
          .child(file.name)
          .getDownloadURL()
          .then((url) => {
            console.log(url);
// where you need your url
setImage(url)
          });
      },
      (error) => {
        reject(error);
      },
      () => {
        uploadTask.snapshot.ref.getDownloadURL().then((url) => {
          console.log('image download url:', url);
// or set image here 
setImage(url)
          resolve(url); // this is important
        });
      }
    );
  });

Upvotes: 1

Faruk
Faruk

Reputation: 444

handleUploadImage function must be return a new url with Promise. Change function like this:

export const handleUploadImage = (image: any, folderName = 'adhaar') =>
  new Promise((resolve, reject) => {
    let file = image;
    const storage = firebase.storage();
    const storageRef = storage.ref();
    const uploadTask = storageRef.child(`${folderName}/` + file.name).put(file);

    uploadTask.on(
      firebase.storage.TaskEvent.STATE_CHANGED,
      (snapshot) => {
        storageRef
          .child(file.name)
          .getDownloadURL()
          .then((url) => {
            console.log(url);
          });
      },
      (error) => {
        reject(error);
      },
      () => {
        uploadTask.snapshot.ref.getDownloadURL().then((url) => {
          console.log('image download url:', url);
          resolve(url); // this is important
        });
      }
    );
  });

Handle the new url and set react state

const ParentComponent = ()=>
{
    const [imageUrl, setImageUrl] =useState()

      const getUrl = ()=>
      {
        handleUploadImage(image).then((newUrl) => setImageUrl(newUrl))
          
      }

return(
   <button onClick={getUrl}> Get Url </button>

)


}

Upvotes: 1

Related Questions