chat-app-react-nodejs icon indicating copy to clipboard operation
chat-app-react-nodejs copied to clipboard

UseEffect Hook Not Working

Open Raghav0407 opened this issue 3 years ago • 6 comments

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)

Raghav0407 avatar Mar 16 '23 07:03 Raghav0407

Let me see your “ destroy” line

vsinh1997 avatar Mar 16 '23 08:03 vsinh1997

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

sharmakhil avatar Apr 19 '23 16:04 sharmakhil

@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.

GRISONRF avatar Apr 26 '23 00:04 GRISONRF

  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

bharathrames avatar Jun 28 '23 10:06 bharathrames

@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 !

Raghav0407 avatar Jun 28 '23 10:06 Raghav0407

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: @.***>

bharathrames avatar Jun 28 '23 13:06 bharathrames