通过React功能组件更新状态复选框

时间:2020-07-16 15:52:20

标签: reactjs react-hooks react-props react-component

每次尝试选中/取消选中复选框时,我都尝试更新渲染的卡片。目前,我在尝试解决如何更新 todos 状态(这是一个包含多个JSON的数组)时花了很多时间:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Python37-32\SqlVersionpr_import.py", line 840, in proceed
Micros ON payrolldata;""")
File "src\pymssql.pyx", line 465, in pymssql.Cursor.execute
pymssql.ProgrammingError: (102, b"Incorrect syntax near 'Micros'.DB-Lib error message 20018, severity 
15:\nGeneral SQL Server error: Check messages from the SQL Server\n")

通过onChange调用 changedBox 时,值会更新,但由于(错误地)试图比较 componentDidUpdate 中的待办事项而不会重新渲染?当我手动刷新页面时,复选框的更新值将再次呈现。有没有一种方法可以更新Todo组件中已检查的待办事项,使其能够正确呈现?

这是我现在拥有的:

[{"_id":"fhrgiu34hnt","todo_description":"Lunch","todo_responsible":"1:30PM","todo_priority":"High","todo_completed":true,"todo_id":"fhrgiu34hnt"},{"_id":"fgvjhbansfgki35sn","todo_description":"Dinner","todo_responsible":"7:45PM","todo_priority":"High","todo_completed":true,"todo_id":"fgvjhbansfgki35sn"},{"_id":"sfgvhio3t58","todo_description":"Supper","todo_responsible":"10:30PM","todo_priority":"Medium","todo_completed":false}]

1 个答案:

答案 0 :(得分:0)

有些事情您可以在这里做得更好。

首先,useState返回2个元素组成的数组,第一个是数据,第二个是updater函数, 因此,每当您使用useState并想要更新状态时,都可以这样:

const [data, setData] = useState('some data');

setData('I want to update it');

然后组件将重新渲染。

此问题应解决

.then(response => {
  // t = props.todo;
  setTodo(props.todo);
})

changedBox方法不会导致组件重新呈现,因为它没有更改状态,请再次使用setTodo

    const obj = {
        todo_id: props.todo._id,
        todo_description: props.todo.todo_description,
        todo_responsible: props.todo.todo_responsible,
        todo_priority: props.todo.todo_priority,
        todo_completed: completed
    };

setTodo(obj)

我看到您正在使用挂钩和基于类的组件的混合,请尝试使用其中之一,这将使您的代码更加一致。