I have problem with react-redux. I create a checkbox with redux, but i have a problem that my data=[array] back data=[array] but not [new array] with new props "completed". When i use this function on reducer, i have problem with my ComponentBike this.props.state.map
inspector says: Cannot read property 'map' of undefined
. What am I doing wrong, people?
This is my reducer:
import { TOGGLE_CHECKBOX } from '../actions/actions'
export default function reducer(state = [], action) {
switch (action.type) {
case 'TOGGLE_CHECKBOX':
return {
...state,
checkbox: state.checkbox.map(check => check.id === action.id ?
{...check, completed: action.completed} :
check
)
};
}
}
This is my action:
export const TOGGLE_CHECKBOX = 'TOGGLE_CHECKBOX';
export function toggleCheckbox(id) {
return {
type: TOGGLE_CHECKBOX,
completed: true,
id
}
}
My Container:
import React from 'react';
import { connect } from 'react-redux';
import { toggleCheckbox } from '../../redux/actions/actions';
import ComponentBike from '../../components/bikes/info-bikes';
function mapStateToProps(state) {
return {
state: state
};
}
function mapDispatchToProps(dispatch) {
return {
onChange: (id) => dispatch(toggleCheckbox(id)),
}
}
const CheckboxContainer = connect(mapStateToProps, mapDispatchToProps)(ComponentBike);
export default CheckboxContainer;
My store:
import { createStore } from 'redux';
import reducer from '../reducers/reducer';
export const initialState = {
'checkbox': [
{
'id': 1,
'label': 'Sport',
'completed': false
},
{
'id': 2,
'label': 'Cross',
'completed': false
}
]
const store = createStore(reducer, initialState);
export default store;
and my component:
import React from 'react';
import Checkbox from '../../../../Checkbox';
export default class ComponentBike extends React.Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
this.props.onChange(this.props.id);
}
render () {
return <div className="window">
<Container>
<div className="all-checks">
{this.props.state.map(check =>
<Checkbox
key={check.id}
id={check.id}
label={check.label}
checked={check.completed}
onChange={this.handleClick(check.id)}
/>
)}
</div>
I would be very grateful for help with this problem.