Quantcast
Channel: Active questions tagged react-native+android - Stack Overflow
Viewing all articles
Browse latest Browse all 28474

How I get state from reducer in action for get name using redux

$
0
0

Here is my code below:

`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;
    }
}

i get updated categorylistData in action file's callApiStockList function where i get obj in response of api.i want to compare categoryID's of categorylistdata and responseJson.data for getName of category from categorylistData.I tried with getState method but not working.Please anyone can help me.


Viewing all articles
Browse latest Browse all 28474

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>