在尝试更新React状态对象时出现问题。

huangapple go评论78阅读模式
英文:

Problem when trying to update the object of a React state

问题

以下是代码的翻译部分:

我正在制作一个待办事项应用程序我试图让"completed"按钮在被点击时能够改变React状态中对象中的"state"从false变为true我不确定如何处理handleChange函数

import { useState } from "react";

import "./App.css";

function App() {
  const [count, setCount] = useState(0);
  const [todos, setTodos] = useState([]);

  const [todosText, setTodosText] = useState("");

  function handleOnChange(e) {
    setTodosText(e.target.value);
  }

  function handleAdd() {
    setTodos((prevState) => {
      return [...prevState, { text: todosText, status: false }];
    });
  }

  function handleOnSubmit(e) {
    e.preventDefault();
  }

  function handleChange() {}

  const showText = todos.map((txt) => (
    <li key={txt.text} className={!txt.status ? "" : "dark"}>
      <span>{txt.text}</span>
      <button onClick={handleChange}>完成</button>
    </li>
  ));

  console.log(todos);
  return (
    <div className="App">
      <h1>待办事项清单</h1>
      <form onSubmit={handleOnSubmit}>
        <input type="text" name="todoItem" onChange={handleOnChange} />
        <button onClick={handleAdd}>添加</button>
      </form>
      <ul>{showText}</ul>
    </div>
  );
}

export default App;

注意:翻译中的"完成"按钮已被翻译为"完成"。

英文:

I am making a Todo app. I am trying to give the completed button the ability to change the value of "state" in the object located in the React state from false to true whenever it is clicked. i am not sure how to go about with the handleChange function.

import { useState } from &quot;react&quot;;
import &quot;./App.css&quot;;
function App() {
const [count, setCount] = useState(0);
const [todos, setTodos] = useState([]);
const [todosText, setTodosText] = useState(&quot;&quot;);
function handleOnChange(e) {
setTodosText(e.target.value);
}
function handleAdd() {
setTodos((prevState) =&gt; {
return [...prevState, { text: todosText, status: false }];
});
}
function handleOnSubmit(e) {
e.preventDefault();
}
function handleChange() {}
const showText = todos.map((txt) =&gt; (
&lt;li key={txt.text} className={!txt.status ? &quot;&quot; : &quot;dark&quot;}&gt;
&lt;span&gt;{txt.text}&lt;/span&gt;
&lt;button onClick={handleChange}&gt;Completed&lt;/button&gt;
&lt;/li&gt;
));
console.log(todos);
return (
&lt;div className=&quot;App&quot;&gt;
&lt;h1&gt;ToDo list&lt;/h1&gt;
&lt;form onSubmit={handleOnSubmit}&gt;
&lt;input type=&quot;text&quot; name=&quot;todoItem&quot; onChange={handleOnChange} /&gt;
&lt;button onClick={handleAdd}&gt;Add&lt;/button&gt;
&lt;/form&gt;
&lt;ul&gt;{showText}&lt;/ul&gt;
&lt;/div&gt;
);
}
export default App;

答案1

得分: 1

检查你的ID与你的待办事项列表ID是否匹配,如果匹配,则将状态设置为true,否则返回相同的待办事项对象。

你的待办事项中没有唯一标识符,所以我添加了它,请参考Codesandbox。

function handleChange(id) {
  setTodos((prevState) =>
    prevState.map((todo) =>
      todo.id === id ? { ...todo, status: true } : todo
    )
  );
}

Codesandbox链接:https://codesandbox.io/s/unruffled-wright-lomhuc?file=/src/App.js:453-615

英文:

Check your id with your todo list id and if both matches then make your status true else return same todo object.

There is no unique identifier in your todo so I added it, please refer the codesanbox

function handleChange(id) {
setTodos((prevState) =&gt;
prevState.map((todo) =&gt;
todo.id === id ? { ...todo, status: true } : todo
)
);
}

Codesandbox: https://codesandbox.io/s/unruffled-wright-lomhuc?file=/src/App.js:453-615

huangapple
  • 本文由 发表于 2023年2月27日 04:52:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574941.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定