Open Raghav0407 opened 1 year ago
Let me see your “ destroy” line
@Raghav0407 I am getting the same error can you please help to resolve it
@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.
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 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 !
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: @.***>
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.