Answer the question
In order to leave comments, you need to log in
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))
}
}
export default (state = {}, action) => {
console.log(action.data) //Тут є дані
switch (action.type) {
case 'GET_COIN_DATA':
return {
...state,
coin: action.data
}
default:
return state
}
}
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
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
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 questionAsk a Question
731 491 924 answers to any question