OokerO
Denoβ€’2y agoβ€’
6 replies
Ooker

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 => 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() hook), but it doesn't show up in the client's console. However if I change the
fetch
function to:
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
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()?
Was this page helpful?