I’ve made a test weather app which has a loading screen with a timeout so it shows the Home screen after 6 sec (this will be replaced by the time the API-fetch is loading). Everything works fine however I’d like the loading screen to fade into the Home screen (now it just snaps right into it). Is there a way of doing this?
Thanks
export default function Home() {
const [isLoading, setIsLoading] = useState(true)
const [temperature, setTemperature] = useState(0)
const [weatherCondition, setWeatherCondition] = useState(null)
const lat = 60
const lon = 24
useEffect(() => {
setTimeout(() => {
fetch(`http://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&APPID=${API_KEY}&units=metric`)
.then(res => res.json())
.then(json => {
setTemperature(json.main.temp)
setWeatherCondition(json.weather[0].main)
setIsLoading(false)
})
.catch((e) => console.log(e))
}, 6000);
},[]);
return (
<View style={styles.container}>
{isLoading ?
<LoadingScreen/>
:
<Weather weather={weatherCondition} temperature={temperature}/>
}
</View>
)
}
2
Answers
You can use react-native-modal for your loading screen and in
animationOut
pass this value'fadeOut'
You can do something like this.
First create a Spinner Component so you can use it wherever you want.
Spinner.js
It take on prop visible to show or hide the ActivityIndicator
Now simply use it in your screen
First import it.
and Use it like this.