Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UseEffect Hook Not Working #20

Open
Raghav0407 opened this issue Mar 16, 2023 · 6 comments
Open

UseEffect Hook Not Working #20

Raghav0407 opened this issue Mar 16, 2023 · 6 comments

Comments

@Raghav0407
Copy link

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
Copy link

Let me see your “ destroy” line

@sharmakhil
Copy link

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

@GRISONRF
Copy link

@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
Copy link

  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
Copy link
Author

Raghav0407 commented Jun 28, 2023

@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
Copy link

bharathrames commented Jun 28, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants