Skip to content

Commit

Permalink
Deal with createSlice
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown committed Sep 3, 2020
1 parent 15dc8f5 commit 185680b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ Learning Vanilla-Redux and React-Redux
- Detail Screen
- Deal with Creact Action
- Deal with CreateReducer
- Deal with configureStore (req : redux toolkit)
- Deal with configureStore (req : redux toolkit)

* Deal with createSlice
4 changes: 2 additions & 2 deletions src/components/ToDo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import { connect } from "react-redux";
import { actionCreators } from "../store";
import { remove } from "../store";
import { Link } from "react-router-dom";

function ToDo({ text, onBtnClick, id }) {
Expand All @@ -14,7 +14,7 @@ function ToDo({ text, onBtnClick, id }) {

function mapDispatchToProps(dispatch, ownProps) {
return {
onBtnClick: () => dispatch(actionCreators.deleteToDo(ownProps.id))
onBtnClick: () => dispatch(remove(ownProps.id))
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/routes/Home.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState } from "react";
import { connect } from "react-redux";
import { actionCreators } from "../store";
import { add } from "../store";
import ToDo from "../components/ToDo";

function Home({ toDos, addToDo }) {
Expand Down Expand Up @@ -35,7 +35,7 @@ function mapStateToProps(state) {

function mapDispatchToProps(dispatch) {
return {
addToDo: text => dispatch(actionCreators.addToDo(text))
addToDo: text => dispatch(add(text))
};
}

Expand Down
26 changes: 19 additions & 7 deletions src/store.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createStore } from "redux";
import { createAction, createReducer, configureStore } from "@reduxjs/toolkit";
import {configureStore, createSlice } from "@reduxjs/toolkit";


/*
Expand All @@ -20,10 +20,11 @@ const deleteToDo = id => {
};
};
*/

/*
const addToDo = createAction("ADD");
const deleteToDo = createAction("DELETE");
/*
const reducer = (state = [], action) => {
switch (action.type) {
case addToDo.type:
Expand All @@ -36,19 +37,30 @@ const reducer = (state = [], action) => {
};
*/

/*
const reducer = createReducer([], {
[addToDo]: (state, action) =>{
state.push({text:action.payload,id:Date.now()});
},
[deleteToDo]: (state, action) =>
state.filter(toDo => toDo.id !== action.payload)
})
*/

const toDos = createSlice({
name:'toDosReducer',
initialState:[],
reducers:{
add: (state, action) =>{
state.push({text:action.payload, id:Date.now()})
},
remove: (state, action) =>
state.filter(toDo => toDo.id !== action.payload)
}
})

const store = configureStore({ reducer });
const store = configureStore({ reducer: toDos.reducer });

export const actionCreators = {
addToDo,
deleteToDo
};
export const {add,remove} = toDos.actions;

export default store;

0 comments on commit 185680b

Please sign in to comment.