2021年4月24日星期六

React Redux Toolkit: How can I can store my initialState to localStorage using createSlice?

I'm a bit too confused about how can I Store the initialState of my taskSlice into my localStorage. Can you please help me with how can I solve this problem? The result that I want is if the app runs the initialState will be store in the localStorage. Coz what I'm doing is using useSelector inside my component then useEffect to know if there's an existing localStorage value. If false it will store the state inside the localStorage.

taskSlice.js

import { createSlice } from '@reduxjs/toolkit';  import { v4 as uuidv4 } from 'uuid';    const initialState = [    {      _id: uuidv4(),      title: 'Learn React',      desc: 'Nec ullamcorper sit amet risus nullam eget felis.',      isProgress: false,      isComplete: false,      priority: 'Minor',      created: 'johndoe',      assigned: 'Doe John',      dateCreated: new Date(),      dateDue: new Date(),    },  ];    export const taskSlice = createSlice({    name: 'tasks',    initialState: initialState,    reducers: {  addTodo: (state, action) => {        state.push({          _id: uuidv4(),          title: action.payload.taskInfo.title,          desc: action.payload.taskInfo.description,          isProgress: false,          isComplete: false,          priority: action.payload.taskInfo.priority,          dateCreated: new Date(),          created: action.payload.taskInfo.created,          assigned: action.payload.taskInfo.assigned,          dateDue: action.payload.taskInfo.dateDue,        });      },    },  });    export const { addTodo } = taskSlice.actions;    export const taskSelector = (state) => state.tasks;    export default taskSlice.reducer;  
https://stackoverflow.com/questions/67248867/react-redux-toolkit-how-can-i-can-store-my-initialstate-to-localstorage-using-c April 25, 2021 at 09:08AM

没有评论:

发表评论