koolkishan / chat-app-react-nodejs

Chat Application Using React, Node.js and Socket.io with MongoDB
https://www.youtube.com/watch?v=otaQKODEUFs
776 stars 555 forks source link

UseEffect Hook Not Working #20

Open Raghav0407 opened 1 year ago

Raghav0407 commented 1 year ago

Sir I have done till Avatar and till that only useEffect hook is not working and it is showing that destroy function is not defined. Can you please tell me the error of the hook. Screenshot (159)

vsinh1997 commented 1 year ago

Let me see your “ destroy” line

sharmakhil commented 1 year ago

@Raghav0407 I am getting the same error can you please help to resolve it

GRISONRF commented 1 year ago

@Raghav0407 @sharmakhil That's because Effect hook callbacks can't be async. So instead of useEffect(async () => { ... } we should declare a callback function scoped async function and invoke it, like:

useEffect(() => {
        const fetchData = async () => { ... fetchData();
      }, []);

That's how I did and it worked.

bharathrames commented 1 year ago
  useEffect(async () => {
    const data = [];
    for (let i = 0; i < 4; i++) {
      const image = await axios.get(
        `${api}/${Math.round(Math.random() * 1000)}`
      );
      const buffer = new Buffer(image.data);
      data.push(buffer.toString("base64"));
    } 
    setAvatars(data);
    setIsLoading(false);

  }, []);

how to change the code like you sayed above

Raghav0407 commented 1 year ago

@bharathrames Ya Sure I can help,

Changed Code: `useEffect( () => {

const sampleFunction = async()=>{ const data = []; for (let i = 0; i < 4; i++) { const image = await axios.get( ${api}/${Math.round(Math.random() * 1000)} ); const buffer = new Buffer(image.data); data.push(buffer.toString("base64"));

} 
setAvatars(data);
setIsLoading(false);

} sampleFunction();

}, []);`

Now I think it will work . Please check !

bharathrames commented 1 year ago

its working , Thank you for your help and immediate response. Once again thank you very

On Wed, Jun 28, 2023 at 4:09 PM Raghav0407 @.***> wrote:

Ya Sure I can help,

Changed Code: useEffect( () => {

const sampleFunction = async()=>{ const data = []; for (let i = 0; i < 4; i++) { const image = await axios.get( ${api}/${Math.round(Math.random() * 1000)} ); const buffer = new Buffer(image.data); data.push(buffer.toString("base64"));

} setAvatars(data); setIsLoading(false);

} sampleFunction();

}, []);

Now I think it will work . Please check !

— Reply to this email directly, view it on GitHub https://github.com/koolkishan/chat-app-react-nodejs/issues/20#issuecomment-1611170748, or unsubscribe https://github.com/notifications/unsubscribe-auth/A5ZOIRI2L7SNAULAYXNTHYLXNQCUXANCNFSM6AAAAAAV42MG4E . You are receiving this because you commented.Message ID: @.***>