Today i tried to implement redux persist to save todos in the local storage.
I have made everything right using documentation, but the problem now is that i have todos in the local storage, but can’t see todos in the app.
Before implementation, everything works fine. Must understand this part of using locaStorage with redux toolkit/redux-persist.
I just leave here the github repo: https://github.com/DanielCapcelea/todo-with-motion
If someone can show where i have made an error. Please or pull request or answer here.
Thx a lot!
store.js
import {combineReducers, configureStore} from "@reduxjs/toolkit";
import {
persistStore,
persistReducer,
FLUSH,
REHYDRATE,
PAUSE,
PERSIST,
PURGE,
REGISTER,
} from 'redux-persist';
import storage from 'redux-persist/lib/storage' // defaults to localStorage for web
import todoReducer from './todoSlice';
const rootReducer = combineReducers({
todoReducer,
});
const persistConfig = {
key: 'root',
storage,
};
const persistedReducer = persistReducer(persistConfig, rootReducer)
const store = configureStore({
reducer: persistedReducer,
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
serializableCheck: {
ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
},
}),
});
export const persistor = persistStore(store);
export default store;
todoSlice.js
import { createSlice } from "@reduxjs/toolkit";
const initialState = [];
const todoSlice = createSlice({
name: "todos",
initialState,
reducers: {
//Adding todos
addTodos: (state, action) => {
state.push(action.payload);
return state;
},
//remove todos
removeTodos: (state, action) => {
return state.filter((item) => item.id !== action.payload);
},
//update todos
updateTodos: (state, action) => {
return state.map((todo) => {
if (todo.id === action.payload.id) {
return {
...todo,
item: action.payload.item,
};
}
return todo;
});
},
//completed
completeTodos: (state, action) => {
return state.map((todo) => {
if (todo.id === action.payload) {
return {
...todo,
completed: !todo.completed,
};
}
return todo;
});
},
},
});
export const {
addTodos,
removeTodos,
updateTodos,
completeTodos,
} = todoSlice.actions;
export default todoSlice.reducer;
displayTodos.js
import React, {useState} from "react";
import {connect} from "react-redux";
import {
completeTodos,
removeTodos,
updateTodos,
} from "../redux/todoSlice";
import TodoItem from "./TodoItem";
import {AnimatePresence, motion} from "framer-motion";
const mapStateToProps = (state) => {
return {
todos: state,
};
};
const mapDispatchToProps = (dispatch) => {
return {
removeTodo: (id) => dispatch(removeTodos(id)),
updateTodo: (obj) => dispatch(updateTodos(obj)),
completeTodo: (id) => dispatch(completeTodos(id)),
};
};
const DisplayTodos = (props) => {
const [sort, setSort] = useState("active");
return (
<div className="displaytodos">
<div className="buttons">
<motion.button
whileHover={{scale: 1.1}}
whileTap={{scale: 0.9}}
onClick={() => setSort("active")}
>
Active
</motion.button>
<motion.button
whileHover={{scale: 1.1}}
whileTap={{scale: 0.9}}
onClick={() => setSort("completed")}
>
Completed
</motion.button>
<motion.button
whileHover={{scale: 1.1}}
whileTap={{scale: 0.9}}
onClick={() => setSort("all")}
>
All
</motion.button>
</div>
<ul>
<AnimatePresence>
{props.todos.length > 0 && sort === "active"
? props.todos.map((item) => {
return (
item.completed === false && (
<TodoItem
key={item.id}
item={item}
removeTodo={props.removeTodo}
updateTodo={props.updateTodo}
completeTodo={props.completeTodo}
/>
)
);
})
: null}
{/* for completed items */}
{props.todos.length > 0 && sort === "completed"
? props.todos.map((item) => {
return (
item.completed === true && (
<TodoItem
key={item.id}
item={item}
removeTodo={props.removeTodo}
updateTodo={props.updateTodo}
completeTodo={props.completeTodo}
/>
)
);
})
: null}
{/* for all items */}
{props.todos.length > 0 && sort === "all"
? props.todos.map((item) => {
return (
<TodoItem
key={item.id}
item={item}
removeTodo={props.removeTodo}
updateTodo={props.updateTodo}
completeTodo={props.completeTodo}
/>
);
})
: null}
</AnimatePresence>
</ul>
</div>
);
};
export default connect(mapStateToProps, mapDispatchToProps)(DisplayTodos);