- Updated the Weather and Project Apps to render data on page load with
useEffect
, so that I don't have to press a button to display the information.
import React, {useEffect, useState} from "react"
function Image({size, search}) {
const [src, setSrc] = useState(null)
const updateImage = () => {
const baseUrl = "https://source.unsplash.com"
const route = search === "" ? "/random" : "/featured"
const url = `${baseUrl}${route}/${size}x${size}?${search}`
fetch(url).then(response => setSrc(response.url))
}
useEffect(() => {
updateImage()
}, [size, search])
return (
<img src={src} onClick={updateImage} />
)
}
export default Image;