Доброго дня!
Пробую сохранить данные из АПИ в хранилище redux, но получаю пустой state:
action.jsimport 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.jsexport default (state = {}, action) => {
console.log(action.data) //Тут є дані
switch (action.type) {
case 'GET_COIN_DATA':
return {
...state,
coin: action.data
}
default:
return state
}
}
Если сменить state -
state.data = action.data - то state не пустой
store.jsimport { 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.jsimport 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;