Im new to React and Redux and still confused a little bit.
My goal is to render json datas in the HTML by using GET request. I'm using react and redux to manage the state of the objects, but I believe my problem is that the data is not even there
here is the code:
Action.js :
import axios from 'axios';
export function fetchUsers(){
const request=axios.get('https://randomuser.me/api/');
return(dispatch)=>{
request.then(({data})=>{
dispatch({type:'FETCH_PROFILES',payload:data})
});
};
}
Component.js:
import React,{Component} from 'react';
import {connect} from 'react-redux';
import * as actions from '../actions';
class App extends Component {
ComponentWillMount() {
this.props.dispatch(onFetchUser());
}
render() {
const mappedUsers = this.props.users.map(user => <li>{user.email}</li>)
return <div>
<ul>{mappedUsers}</ul>
</div>
}
}
const mapStateToProps = (state) => {
return {
users: state.users.users
}
}
const mapDispatchToProps = (dispatch) => {
return {
onFetchUsers: () => dispatch(fetchUsers())
};
}
export default connect(mapStateToProps, mapDispatchToProps)(App);
CombineReducer.js :
import { combineReducers } from "redux"
import users from "./reducer-active-user"
export default combineReducers({
users ,
})
Reducer.js :
const initialState = {
users: []
};
export default function reducer(state= initialState
, action) {
switch(action.type){
case 'FETCH_PROFILES':
return {
users: action.payload,
}
break;
}
return state;
}
Any help would be appreciated.