单击反应中的元素时如何更改 LocalStorage 中存储的数据?

发布于 2025-01-10 00:03:05 字数 276 浏览 1 评论 0原文

我正在反应中创建待办事项应用程序,并将数据存储在本地存储中,当用户单击特定任务时,它被标记为已完成,为此目的,我在本地存储中的所有任务都有“完整”布尔属性。现在我想更改它该特定任务的 onclick 属性,如何实现这一点?。这里是代码链接: https://github.com/Khatri-Jinal/react-app/tree/实用4

I am creating to-do app in react, and storing the data in localstorage,when user click on particular task it is mark completed, for that purpose i have "complete" boolean property for all task in localStorage.now i want to change that property onclick of that particular task,How to achieve this?.Here is the code link :
https://github.com/Khatri-Jinal/react-app/tree/practical4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

勿挽旧人 2025-01-17 00:03:05

从本地存储获取值并更新它,然后再次设置它
例如:
let data=localStorage.getItem('tasks')
//对数据进行更改
localStorage.setItem("tasks", JSON.stringify(tasks));

get the value from local storage and update it and then set it again
for ex:
let data=localStorage.getItem('tasks')
//make changes in data
localStorage.setItem("tasks", JSON.stringify(tasks));

め可乐爱微笑 2025-01-17 00:03:05

我建议您为本地存储创建一个自定义挂钩(即 useLocalStorage)。这可确保当您更新本地存储中的值时,使用更新值的组件会自动重新渲染。

您可以在线查找或使用此 YouTube 视频作为参考。第一个例子是 useLocalStorage。

编辑:
您的任务变量应该是一个对象数组。渲染任务时,在 onclick 函数上传递一个 id 或该任务特有的任何内容(在本例中,我将只使用任务名称,但我建议创建您自己的任务名称)。

// tasks hook
const [tasks, setTasks] = useState([
  {desc: 'jog', isComplete: true},
  {desc: 'walk', isComplete: false},
  {desc: 'read', isComplete: true},
]);

// rendering tasks
tasks.map(task => {
  <div key={task.desc} onClick={() => onClickTask(task.desc)}
    task.desc
  </div>
});

这是你的 onClickTask 函数

const onClickTask = (identifier) => {
  const index = tasks.findIndex(task => task.desc === identifier);
  const newTasks = [...tasks];
  newTasks[index].isComplete = true;

  setTasks(newTasks);
};

I suggest you make a custom hook for local storage (ie. useLocalStorage). This ensures that when you update a value in the local storage, the components that are using the updated value are automatically re-rendered.

You may look it up online or use this youtube viode for your reference. The first example there is useLocalStorage.

EDIT:
Your task variable should be an array of object. When rendering the task, pass an id or anything unique to the task on the onclick function (In this example, I'll just use task name but I advise to create your own).

// tasks hook
const [tasks, setTasks] = useState([
  {desc: 'jog', isComplete: true},
  {desc: 'walk', isComplete: false},
  {desc: 'read', isComplete: true},
]);

// rendering tasks
tasks.map(task => {
  <div key={task.desc} onClick={() => onClickTask(task.desc)}
    task.desc
  </div>
});

And this is your onClickTask function

const onClickTask = (identifier) => {
  const index = tasks.findIndex(task => task.desc === identifier);
  const newTasks = [...tasks];
  newTasks[index].isComplete = true;

  setTasks(newTasks);
};
帅气称霸 2025-01-17 00:03:05

我提出了这个解决方案,希望它有所帮助。

  const [todoList, setTodoList] = useState([]);

  // Get todo list from localStorage when the component is mounted
  useEffect(() => {
    setTodoList(JSON.parse(localStorage.getItem('todoList')));
  }, [])

  function updateTodo(todoId, data) {
    const todoIndex = todoList.findIndex(todo => todo.id === todoId);
    // Get a copy of the todo
    let updatableTodo = { ...todoList[todoIndex] };

    // Update the todo here...
    updatableTodo.title = data.title;

    // Update the state and localStorage
    setTodoList(todoList => {
      const newTodoList = [...todoList];
      newTodoList[todoIndex] = updatableTodo;
      localStorage.setItem('todoList', JSON.stringify(newTodoList));
      return newTodoList;
    })
  }

  return (
    <div>
      {todoList.map((todo) => 
        <button onClick={() => updateTodo(todo.id, data)} key={todo.id}>Update</button>
      )}
    </div>
  );

I made this solution, I hope it helps.

  const [todoList, setTodoList] = useState([]);

  // Get todo list from localStorage when the component is mounted
  useEffect(() => {
    setTodoList(JSON.parse(localStorage.getItem('todoList')));
  }, [])

  function updateTodo(todoId, data) {
    const todoIndex = todoList.findIndex(todo => todo.id === todoId);
    // Get a copy of the todo
    let updatableTodo = { ...todoList[todoIndex] };

    // Update the todo here...
    updatableTodo.title = data.title;

    // Update the state and localStorage
    setTodoList(todoList => {
      const newTodoList = [...todoList];
      newTodoList[todoIndex] = updatableTodo;
      localStorage.setItem('todoList', JSON.stringify(newTodoList));
      return newTodoList;
    })
  }

  return (
    <div>
      {todoList.map((todo) => 
        <button onClick={() => updateTodo(todo.id, data)} key={todo.id}>Update</button>
      )}
    </div>
  );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文