redux를 왜씀?
-전역에서 state관리를 해줄수 있기때문
전역에서 state관리하는게 편하냐?
-props를 통해 state드릴링을 할필요가 없기때문
스토어가 object 가아닌 다른 형식(promis, function)의 데이터도 받을수 있도록하는 미들웨어
export const DEFAULTACTIONS = "defatulActions";
export const DEFAULTACTIONSNAMECHANGE = "defatulActionsNameChange";
import { DEFAULTACTIONS, DEFAULTACTIONSNAMECHANGE } from "./actionTypes";
export const defautdActions = () => {
// axios같은 promise의 값을 받아 return에 추가 할수도 있다.
return { type: DEFAULTACTIONS };
};
export const defautdActionNameChange = () => {
return { type: DEFAULTACTIONSNAMECHANGE };
};
import { DEFAULTACTIONS, DEFAULTACTIONSNAMECHANGE } from "./actionTypes";
type ActionType = { type: string };
const defaultState = {
result: "기본",
};
const defaultReducer = (state = defaultState, action: ActionType) => {
// 만약 actions에서 프로미스 객체를 반환한 값을 받을 수 있다.
console.log(action);
switch (action.type) {
case DEFAULTACTIONS:
return { ...state };
case DEFAULTACTIONSNAMECHANGE:
return { ...state, result: "default" };
default:
return state;
}
};
export default defaultReducer;
//.. 여러 임포트들
import promiseMiddleware from "redux-promise";
import ReduxThunk from "redux-thunk";
import { Provider } from "react-redux";
import { applyMiddleware, createStore } from "redux";
import reducers from "./Redux/combineReducers";
import logger from "redux-logger";
const store = createStore(reducers, applyMiddleware(promiseMiddleware, ReduxThunk, logger));
ReactDOM.render(
<React.StrictMode>
<Provider store={store}>
<App />
</Provider>
</React.StrictMode>,
document.getElementById("root")
);
reportWebVitals();
const dispatch=useDispatch()
dispatch(defautdActions())