DenoDDeno
Powered by
OokerO
Denoβ€’2y agoβ€’
6 replies
Ooker

Why does `res.text()` not work inside `useEffect()`?

On
islands/test.tsx
islands/test.tsx
I have:
import { useEffect } from "preact/hooks";
export default function Test(){ 
    fetch('https://example.com')
    .then(res => res.text())
    .then(d => console.log(d))
    
    useEffect(() => {        
        fetch('https://example.com')
        .then(res => res.text())
        .then(d => console.log(d))     
    })
    return 'hi'
}
import { useEffect } from "preact/hooks";
export default function Test(){ 
    fetch('https://example.com')
    .then(res => res.text())
    .then(d => console.log(d))
    
    useEffect(() => {        
        fetch('https://example.com')
        .then(res => res.text())
        .then(d => console.log(d))     
    })
    return 'hi'
}

The data returns fine in the server's console (the part outside the
useEffect()
useEffect()
hook), but it doesn't show up in the client's console. However if I change the
fetch
fetch
function to:
fetch("https://jsonplaceholder.typicode.com/todos/1")
.then(res => res.json())
.then(d => console.log(d))
fetch("https://jsonplaceholder.typicode.com/todos/1")
.then(res => res.json())
.then(d => console.log(d))

then it shows on both sides. Why is that the case?

(Also asked on Stack Overflow).
Stack Overflow
Why does res.text() not work inside useEffect()?
On islands/test.tsx I have:
import { useEffect } from "preact/hooks";
export default function Test(){
fetch('https://example.com')
.then(res => res.text())
.then(d => c...
Why does res.text() not work inside useEffect()?
Deno banner
DenoJoin
Chat about Deno, a modern runtime for JavaScript and TypeScript.
20,944Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

fetch does not work
Ziga ZajcZZiga Zajc / help
16mo ago
Why Does Firebase JS SDK work on Deno
tatsuTtatsu / help
16mo ago
Deno does not work with typescript 5 decorators
AapoAlasAAapoAlas / help
3y ago