I'm currently revising my understanding of redux and react. React components are working, and I have a knex database setup along with it. I have a reducer using the following syntax.
import { FETCH_POSTS, NEW_POST } from '../actions/types';
const initialState = {
items: [],
item: {}
}
export default function(state = initialState, action) {
switch (action.type) {
case FETCH_POSTS:
return {
...state,
items: action.payload
};
case NEW_POST:
return {
...state,
item: action.payload
};
default:
return state;
}
}
Here is an error I get. It doesn't seem to like the spread operator.
bundle.js:445 Uncaught Error: Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: C:/workspace/study-related-projects/apistudy7112018/1.0/client/reducers/postReducer.js: Unexpected token (18:8)
It doesnt like the current syntax Im using, which was used from this video tutorial from Traversy Media here. The tutor implements the middleware differently to how I've done it, but didn't think that would be a problem. So just incase, I decided to push to the items array like so in the reducer:
import { FETCH_POSTS, NEW_POST } from '../actions/types';
const initialState = {
items: [],
item: {}
}
export default function(state = initialState, action) {
switch (action.type) {
case FETCH_POSTS:
return state.items.push(action.payload)
default:
return state;
}
}
instead of the return object with the spread operator.
and then call dispatch in the componentWillMount() function in react..
Which turned out fine, however now it has become more difficult to map the array for printing out the data in a component.
If I console log the this.props.payload.posts, I recieve the items but Im unsure if I can access it because the next array after posts has no name, just number 0. I tried this.props.payload.posts[0], but that didn't seem to work. This is giving me trouble mapping the data with the map array function.
Generally, If all is successful I can then use mapToProps function, then:
this.props.items.posts.map(post) => {
return post.name
})
But I've hit a wall with these two solution. This problem came about due to me trying to implement a different way of using redux, as opposed to the way I was used to before. This is all for my own study and training.
Could anyone help me understand these two problems? One is that, javascript doesn't like the spread operator syntax I'm using and spits back that error. Second is, trying to get to the raw data through the items array. If anyone could help me out, I'd really appreciate it. Thanks.
Edit: I may put up a git repo in a bit so everyone can see the code I'm working on.

itemsin one case anditemin the other, which marries up with the error message, which is telling you the error is on line 18. I would suggest you need to useitemson line 18.