I'm learning to use react and redux.
So I have a little bit of questions:
I have structured in this way:
reducers.js
const initialState = {
meetings: []
}
export const reducer = (state = initialState, action) => {
switch(action.type){
case 'MEETINGS_LIST':
return [...state.meetings, action.meetings];
default:
return state;
}
}
export default reducer;
action.js
export const meetingsList = (meeting) => {
return(dispatch) => {
fetch(`https://jsonplaceholder.typicode.com/users`)
.then(
res => res.json(),
error => console.log('An error occurred.', error))
.then(meetings => {
dispatch({
type: 'MEETINGS_LIST',
meetings: meetings
})
})
}
}
It's right to do the fetch in the actions.js? Or I should do in the reducers?
Anyway my problem is in main page,
class MeetingsList extends Component {
constructor(props){
super(props);
this.state = {}
}
componentDidMount(){
this.props.meetingsList();
}
render(){
console.log(this.props.meetings)
return(
<div>
</div>
)
}
}
function mapStateToProps(state){
return {meetings: state}
};
function mapDdispatchToProps(dispatch){
return {
meetingsList: (meetings) => dispatch(meetingsList(meetings))
}
}
export default connect(mapStateToProps, mapDdispatchToProps)(MeetingsList)
I would like to print the contents of the props on the screen. It is an array [10], I had thought of using a map to be able to print every single array, but every time I try it tells me that it is impossible to use the map of an undefined. How can I do?
Thanks