I have the following code snippets:
action creator:
export function addCategories(cats){
return {
type: ADD_CATEGORY,
cats
}
}
reducer:
function posts(state = {posts: []}, action) {
switch (action.type) {
case ADD_POST: {
console.log(action.posts)
return {...state, posts: [...state['posts'], Object.assign({}, action.posts)]}
}
default:
return state
}
}
component:
import React, { Component } from 'react';
import { connect } from 'react-redux'
import { addCategories, addPosts } from './actions/index'
class App extends Component {
render() {
console.log(this.props)
return (
<div>
<button onClick={() => { this.props.cats({ name: "stra" }) }}>Add cat</button>
<button onClick={() => { this.props.posts({ age: 23 }) }}>Add post</button>
</div>
);
}
}
const mapStateToProps = (state) => {
console.log(state)
return {
state
}
}
const mapDispatchToState = (dispatch) => {
return {
cats: (cat) => dispatch(addCategories(cat)),
posts: (post) => dispatch(addPosts(post))
}
}
export default connect(mapStateToProps, mapDispatchToState)(App)
the propblem i have is that after the reducer is done, instead of getting the state looking like
{
// other values
posts: [] <- values go where
}
i get the state looking like this:
{
// other values
posts: {
posts: []
}
}
the image above is just what the console prints out when i run the code. Any advice on this would be greatly appreciated as i dont know where im going wrong.
Thanks in advance.
