I have a weather application that, when launched through a request, determines the city by IP and saves it to the store, and immediately after that the weather for this city should be displayed
here is my code
const { isPending, days, error, currentDayWeather } =
useTypedSelector((state) => state.weather)
const weatherCity = useTypedSelector(
(state) => state.weather.weather?.city,
)
const userCity = useTypedSelector(
(state: any) => state.location?.location?.city?.name_en,
)
const { service } = useTypedSelector(
(state) => state.service,
)
useLayoutEffect(() => {
dispatch(getGeolocation())
}, [])
useEffect(() => {
dispatch(getWeather(userCity))
}, [userCity])
useEffect(() => {
setSelected(0)
}, [weatherCity])
if (isPending || !weatherCity) return <Loading />
if (!days || error) return <ErrorLoader />
return (
<WeatherWrapper>
<WeatherMain
params={weatherCity}
currentWeather={currentDayWeather}
/>
<WeatherListCard>
{days.map((item: ResultDaysType, index: number) => (
<WeatherItem
key={uuidv4()}
day={index}
weatherData={item}
active={selected === index}
onChangeSelected={setSelected}
/>
))}
</WeatherListCard>
{service === SERVICES[1] && <ChartWeather />}
</WeatherWrapper>
)
}
when I check with react dev tools, the status is filled with correct data, but when I first run the error field is true
and because of this, the application does not start, and when the page is reloaded, everything is fine
maybe it has something to do with using redux-persist?
store.ts
...
const persistedReducer = persistReducer(persistConfig, rootReducer)
const configureStore = () =>
createStore(
persistedReducer,
composeEnhancers(applyMiddleware(thunk)),
)
const store = configureStore()
const persistor = persistStore(store)
export { store, persistor }
Reducers
export const locationReducer = (
state = initialState,
action: { location: any, type: string },
): initialStateType => {
const { location, type } = action
switch (type) {
case TYPES.GET_LOCATION:
return {
...state,
location
}
default: {
return state
}
}
}
export const weatherReducer = (
state = initialState,
action: any,
): initialStateType => {
const {
weather, days, isPending, error, type,
} = action
switch (type) {
case TYPES.SET_WEATHER:
return {
...state,
weather,
days
}
case TYPES.SET_WEATHER_HOURLY:
return {
...state,
weatherHourly: weather,
}
case TYPES.SET_PENDING:
return {
...state,
isPending
}
case TYPES.SET_ERROR:
return {
...state,
error
}
case TYPES.CHANGE_DAY:
return {
...state,
currentDayWeather: state?.days![action.payload] ?? false,
weatherOnHours: {
temp: state?.days![action.payload].weather.map((temp) => Math.round(+temp.main.temp)) ?? false,
date: state?.days![action.payload].weather.map((temp) => temp.dt_txt) ?? false,
},
}
default: {
return state
}
}
}
thunk
export const getWeather = (city: string): ThunkType<ActionsTypes<typeof actions>> => async (dispatch) => {
try {
dispatch(actions.setPending(true))
dispatch(actions.setError(false))
const weather = await weatherApi.getWeather(city)
dispatch(actions.setWeather(weather))
dispatch(actions.changeDay(0))
} catch (error: unknown) {
dispatch(actions.setError(true))
} finally {
dispatch(actions.setPending(false))
}
}