I'm fetching data from a local server, I want to put the response into images so I can render those urls afterwards
const [images, setImages] = useState([])
useFocusEffect(
React.useCallback(()=>{
let id = '62432174021cfe441eb49bf7'
findImages(id)
.then(res=>{
console.log(res.content)
setImages(res.content)
console.log(images)
})
},[])
)//useEffect
you see that console.log(res.content) ? it shows in console the following:
Array [
Object {
"__v": 0,
"_id": "6243499ae6b17dc7de44fb0c",
"chapterid": "62432174021cfe441eb49bf7",
"url": "https://firebasestorage...",
},
Object {
"__v": 0,
"_id": "624349b0e6b17dc7de44fb0e",
"chapterid": "62432174021cfe441eb49bf7",
"url": "https://firebasestorage...",
},
Object {
"__v": 0,
"_id": "624349b7e6b17dc7de44fb10",
"chapterid": "62432174021cfe441eb49bf7",
"url": "https://firebasestorage...",
},
Object {
"__v": 0,
"_id": "624349bde6b17dc7de44fb12",
"chapterid": "62432174021cfe441eb49bf7",
"url": "https://firebasestorage...",
},
]
And you see that console.log(images) ? it's placed 2 lines after and it shows in console the following: Array [], So I'm getting a proper response from the server but when I try to put it into the useState variable it doesn't work, the funny thing is that when I Ctrl + S the project then console.log(images) gets the same display in the console as console.log(res.content), which is what I want to happen without having to save the project.
I need that image to set the res.content from the start, I watched this video https://www.youtube.com/watch?v=HvM9Ob4CHa0 the boy is doing the same as me but it works for him, look in the min 1:45, line: 30, I don't know what's going on.
this is an interesting thread that I found Why is useState() not setting the default value? but I dont't think it's the same as my scenario, I think.