R
R
Roman2020-03-01 01:40:18
React
Roman, 2020-03-01 01:40:18

Loading state not updating in redux reducer?

Hello, for the first time I started to rewrite my old project on Redux, which I recently met, I rewrote all the functionality that I had associated with api (I connected redux-thunk) and it works only one BUT. the action associated with loading is not called, the state in the reducer with loading: false does not change to true, with any rerender, is it possible to be due to the fact that I have different loading in different reducers? although none of them work. The SET_LOADING action doesn't work for me. Here is the code for one of the reducers:

import Types from '../types';

const INITIAL_STATE = {
  movieData: {},
  trailer: {},
  error: false,
  loading: false
};

const mediaReducer = (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case Types.GET_MOVIE_DATA:
      return {
        ...state,
        movieData: action.payload,
        loading: false
      };
    case Types.GET_TRAILER:
      return {
        ...state,
        trailer: action.payload
      };
    case Types.GOT_ERROR:
      return {
        ...state,
        error: true,
        loading: false
      };
    case Types.SET_LOADING:
      return {
        ...state,
        loading: true // должен на true поменятся, но он не вызывается
      };
    default:
      return state;
  }
};
export default mediaReducer;


redux-logger middleware: does not see this action being called, nor does it see the value output to the console:
5e5ae706b4d28619512776.jpeg

Here is the action code:
import Types from '../types';

import MovieService from '../../services/movie-service';

const {
  getMediaById,
} = new MovieService();

export const getMovieData = movieId => async dispatch => {
  try {
    setLoading(); // вот вызов

    const data = await getMediaById(movieId, 'movie');

    dispatch({
      type: Types.GET_MOVIE_DATA,
      payload: data
    });
  } catch (error) {
    dispatch({
      type: Types.GOT_ERROR,
      payload: error.response.statusText
    });
  }
};


const setLoading = () => {
  return { type: Types.SET_LOADING };
};  /// вот код экшена который не срабатывет, он у меня вызывается сверху, может эго надо както по .другому вызывать?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Cheremkhin, 2020-03-01
@Isildur12

export const getMovieData = movieId => async dispatch => {
  try {
    dispatch(setLoading()); // dispatch пропал у вас

    const data = await getMediaById(movieId, 'movie');
    ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question