

import { ActionType } from "../action-types";
import { Dispatch } from "redux";
import { Action } from "../actions/index";
export const depositMoney = (amount: number) => {
return (dispatch: Dispatch<Action>) => {
dispatch({
type: ActionType.DEPOSIT,
payload: amount,
});
};
};
export const withdrawMoney = (amount: number) => {
return (dispatch: Dispatch<Action>) => {
dispatch({
type: ActionType.WITHDRAW,
payload: amount,
});
};
};
export const bankrupt = () => {
return (dispatch: Dispatch<Action>) => {
dispatch({
type: ActionType.BANKRUPT,
});
};
};
export enum ActionType {
DEPOSIT = "deposit",
WITHDRAW = "withdraw",
BANKRUPT = "bankrupt",
}
import { ActionType } from "../action-types";
interface DepositAction {
type: ActionType.DEPOSIT;
payload: number;
}
interface WithdrawAction {
type: ActionType.WITHDRAW;
payload: number;
}
interface BankruptAction {
type: ActionType.BANKRUPT;
}
export type Action = DepositAction | WithdrawAction | BankruptAction;
import { ActionType } from "../action-types";
import { Action } from "../actions/index";
const initialState = 0;
const reducer = (state: number = initialState, action: Action) => {
switch (action.type) {
case ActionType.DEPOSIT:
return state + action.payload;
case ActionType.WITHDRAW:
return state - action.payload;
case ActionType.BANKRUPT:
return 0;
default:
return state;
}
};
export default reducer;
import { combineReducers } from "redux";
import bankReducers from "./bankReducers";
const reducers = combineReducers({
bank: bankReducers,
});
export default reducers;
export type State = ReturnType<typeof reducers>;
export * as actionCreator from "./action-creators";
export * from "./store";
export * from "./reducers/index";
import { createStore, applyMiddleware } from "redux";
import reducers from "./reducers/index"
import thunk from 'redux-thunk'
export const store = createStore(
reducers,
{},
applyMiddleware(thunk)
)
import React from "react";
import "./App.css";
import { useDispatch, useSelector } from "react-redux";
import { bindActionCreators } from "redux";
import { actionCreator, State } from "./state";
const App = () => {
const dispatch = useDispatch();
const { depositMoney, withdrawMoney, bankrupt } = bindActionCreators(
actionCreator,
dispatch
);
const amount = useSelector((state: State) => state.bank);
return (
<div className="App">
<h1>{amount}</h1>
<button onClick={() => depositMoney(1000)}>Deposit</button>
<button onClick={() => withdrawMoney(500)}>Withdraw</button>
<button onClick={() => bankrupt()}>Bankrupt</button>
</div>
);
};
export default App;
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import { Provider } from "react-redux";
import { store } from "./state/index";
ReactDOM.render(
<React.StrictMode>
<Provider store={store}>
<App />
</Provider>
</React.StrictMode>,
document.getElementById("root")
);
이거 좀 너무 복잡한데..