0

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 enter image description here

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))
  }
}
qwfrjk
  • 71
  • 2
  • 10
  • You should show your store config – Konrad Oct 11 '22 at 19:25
  • I guess that `initialState` has `error` set to `true` or you thunk sets it to `true` – Konrad Oct 11 '22 at 19:33
  • @KonradLinkowski Your advice helped to avoid reloading the page for display, but I have implemented it so that as the city was determined, it is entered in the search input, but this does not happen at the first render, only at the next – qwfrjk Oct 11 '22 at 19:58

0 Answers0