A
A
Aleksandr19_932019-03-21 22:58:12
React
Aleksandr19_93, 2019-03-21 22:58:12

Issues with requesting an API through a Redux store?

Good day!
I'm trying to save data from the API to the redux store, but I get an empty state:
action.js

import axios from 'axios';

function getData (data){
  return {
    type: 'GET_COIN_DATA',
    data
  }
}
export function getCoinData(){
  return function (dispatch){
    return axios
    .get('https://api.coinmarketcap.com/v1/ticker/?limit=5')
    .then(response => {
      dispatch(getData(response.data)) 
    })
    .catch(err => console.log(err))
  }
}

reducer.js
export default (state = {}, action) => {
    console.log(action.data) //Тут є дані
    switch (action.type) {
         case 'GET_COIN_DATA':
         return {
            ...state,
            coin: action.data
         } 
         default:
            return state
     }
}

If you change state - state.data = action.data - then state is not empty
store.js
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import allReducer from '../reducers/index';

let store = createStore(allReducer, applyMiddleware(thunk));

console.log(store.getState())//а тут немає
export default store

Home.js
import Currency from './../layouts/Currency';
import { Provider } from 'react-redux';
import store from './../../store/store';

class Home extends Component {
   render() {
    return (
      <div className="wrapper">
       <h1 className="wrapper-title">Home</h1>
       <ul className="row">
         <li className="row-title">
           <ul className="col">
                <li className="col_img"></li>
                <li className="col_name">Name</li>
                <li className="col_price">Price</li>
                <li className="col_vol">Volume (24h)</li>
           </ul>
         </li>
         <Provider store={store}>
          <Currency />
         </Provider>
       </ul>
      </div>
    );
  }
}

export default Home;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ihor Gevorkyan, 2019-03-22
@Igor-Maf

In short, if by eye, then you, along the way, missed the break statements in the switch of the reducer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question