Когда я нажимаю кнопку увеличения или уменьшения, я получаю это сообщение .Я думаю, что все шаги в порядке, но я не могу понять, почему, когда я нажимаю кнопку (+ или -), хранилище / состояние устанавливается на undefined
;
src / App.js
import React, { Component } from "react";
import Counter from "./components/counter";
import { connect } from "react-redux";
class App extends Component {
render() {
return (
<div>
{this.props.data.map(counter => (
<Counter key={counter.id} id={counter.id} value={counter.value} />
))}
</div>
);
}
}
const mapStateToProps = state => {
return {
data: state.data
};
};
export default connect(mapStateToProps)(App);
src / components / counter.js
import React, { Component } from "react";
import { connect } from "react-redux";
class Counter extends Component {
render() {
return (
<div>
<h1>{this.props.value}</h1>
<button onClick={() => this.props.onIcrement(this.props.id)}>
+ UP
</button>
<button onClick={() => this.props.onDecrement(this.props.id)}>
- DOWN
</button>
</div>
);
}
}
const mapDispatchToProps = dispatch => {
return {
onIcrement: id => dispatch({ type: "INCREMENT", key: id }),
onDecrement: id => dispatch({ type: "DECREMENT", key: id })
};
};
export default connect(
null,
mapDispatchToProps
)(Counter);
src / store / reducer.js
const initialState = {
data: [{ id: 1, value: 4 }, { id: 2, value: 0 }]
};
const reducer = (state = initialState, action) => {
const newState = { ...state };
switch (action.type) {
case "INCREMENT":
return newState.data.map(el => {
if (action.key === el.id) {
return el.value++;
}
return el.value;
});
case "DECREMENT":
return newState.data.map(el => {
if (action.key === el.id) {
return el.value--;
}
return el.value;
});
default:
return newState;
}
};
export default reducer;
src / index.js
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import * as serviceWorker from "./serviceWorker";
import { createStore } from "redux";
import { Provider } from "react-redux";
import reducer from "./store/reducer";
const store = createStore(reducer);
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById("root")
);
serviceWorker.unregister();
................................................................................