import { combineReducers } from "redux";
// reducer を読み込む
import number from "./number";
import title from "./title";
import day from "./day";
// combineReducer で
// 一つの Reducer にまとめる
export default combineReducers({
number,
title,
day
});
index.js
import React from "react";
import { render } from "react-dom";
import { createStore } from "redux";
// まとめ上げた reducer を import する
// 名前が変わっているので注意
import rootReducer from "./reducers";
import { Provider } from "react-redux";
import App from "./containers/App";
// 名前を変更しています
const store = createStore(rootReducer);
render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById("root")
);
containers/App.js
import App from "../components/App";
import { connect } from "react-redux";
import { minus, plus } from "../actions";
// 渡す state を増やす
// state.プロパティ名 という形で、
// 一本化された state にアクセスします
const mapStateToProps = state => {
return {
number: state.number,
day: state.day,
title: state.title
};
};
const mapDispatchToProps = dispatch => {
return {
plus: num => {
dispatch(plus(num));
},
minus: num => {
dispatch(minus(num));
}
};
};
export default connect(mapStateToProps, mapDispatchToProps)(App);