Skip to content

Commit

Permalink
formReset 을 extraReducer를 사용해서
Browse files Browse the repository at this point in the history
  • Loading branch information
hi6724 committed Dec 11, 2022
1 parent 1e1ad38 commit 5eef6e4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
2 changes: 0 additions & 2 deletions cars/src/components/CarForm.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useDispatch, useSelector } from "react-redux";
import { addCar, changeCost, changeName } from "../store";
import { reset } from "../store/slices/formSlice";

function CarForm() {
const dispatch = useDispatch();
Expand All @@ -18,7 +17,6 @@ function CarForm() {
const handleSubmit = (e) => {
e.preventDefault();
dispatch(addCar({ name, cost }));
dispatch(reset());
};

return (
Expand Down
7 changes: 4 additions & 3 deletions cars/src/store/slices/formSlice.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createSlice } from "@reduxjs/toolkit";
import { addCar } from "./carsSlice";
const initialState = {
name: "",
cost: 0,
Expand All @@ -14,9 +15,9 @@ const formSlice = createSlice({
changeCost(state, action) {
state.cost = action.payload;
},
reset(state, action) {
return initialState;
},
},
extraReducers(builder) {
builder.addCase(addCar, (state, action) => initialState);
},
});

Expand Down

0 comments on commit 5eef6e4

Please sign in to comment.