我对如何将我的任务切片的初始状态存储到我的本地存储中有点太困惑了。你能帮我解决这个问题吗?我想要的结果是,如果应用程序运行,initialState 将存储在 localStorage 中。因为我正在做的是在我的组件中使用 useSelector 然后使用 useEffect 来了解是否存在现有的 localStorage 值。如果为 false,它将把状态存储在 localStorage 中。
任务切片.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) …Run Code Online (Sandbox Code Playgroud) 我有这个问题
无法读取未定义的属性“标题”。
我已经按照文档中的示例代码进行操作,react-hook-form但我仍然遇到此问题。
const [taskInfo, setTaskInfo] = useState({
title: '',
description: '',
created: currentUser.fullname,
assigned: '',
priority: '',
dateDue: new Date(),
});
const handleChange = (e) => {
const { name, value } = e.target;
setTaskInfo((prev) => ({ ...prev, [name]: value }));
};
const { register, errors, handleSubmit } = useForm();
<TextField
fullWidth
variant='outlined'
label='Title'
value={taskInfo.title}
onChange={handleChange}
onFocus={handleDisbleLabel}
onBlur={handleEnableLabel}
InputLabelProps={{ shrink: false }}
{...register('title', { required: 'This is required' })}
/>
{errors.title && 'Your input is required'}
Run Code Online (Sandbox Code Playgroud)