我如何从reduce中获取状态以使用redux获取名称

时间:2019-11-19 11:14:56

标签: android ios reactjs react-native

以下是我的代码:

`action.JS`

export const stockList = (id) => (dispatch) => {

    dispatch({ type: STOCK_LIST });

    // alert('api called')
    callApiStockList(dispatch)
};

const callApiStockList = (dispatch) => {
    const URL = 'http://dashboardstaging.africanmanagers.org:8084/api/v1/stocks/10'
    fetch(URL, {
        method: 'GET',
        headers: '',
    })
        .then(response => response.json())
        .then(responseJson => {

            if (responseJson.data !== undefined) {

                const obj = {               
               data: responseJson.data
                }

                getStockListSuccess(dispatch ,obj);

              } 
              else 
              {
                // alert('failed')
                getStockListFail(dispatch, 'get stock list failed');
              }  
          })
        .catch(error => {
            console.log('ERROR:', error);
            getStockListFail(dispatch, "stock list failed");
        });
};

`reducer.JS`

import {
    CATEGORY_LIST_SUCCESS,
    CATEGORY_LIST_FAIL,
    CATEGORY_LIST

} from '../actions/types';

const INITIAL_STATE = {
    categorylistData:[],
    loading: false,
    error: ''
};

export default (state = INITIAL_STATE, action) => {

    switch (action.type) {

        case CATEGORY_LIST:
            return { ...state, loading: true, error: '' }

        case CATEGORY_LIST_SUCCESS:
            return { ...state, loading:false, error: '', categorylistData:action.payload.data }

        case CATEGORY_LIST_FAIL:
            return { ...state, loading:false, categorylistData :[] }

        default:
            return state;
    }
}

我在操作文件的callApiStockList函数中获取更新的categorylistData,其中我得到obj作为api.i的响应,我想比较categorylistdata的categoryID和responseJson.data以获取categorylistData中的类别的getName。我尝试了getState方法,但没有工作。请任何人都可以帮助我。

0 个答案:

没有答案