我正在使用React开发一个小的电话簿网页,当添加人员或更新人员信息时,我希望在页面上显示一条小通知。我通过使用React挂钩来做到这一点,以便为变量message和errorMessage提供状态。
更新后,有问题的消息会接收其内容(例如“添加了新联系人”),setTimeout(() => {setMessage('')}, 6000)并用于清除该消息。
我试图用关键字“ TypeError,setTimeout”等搜索找到的每个线程,但没有结果。我什至不明白是什么问题。如果完全删除setTimeout,一切正常(消息不会消失)。
下面的代码不是我使用的代码,但确实包含相同的问题,即setTimeout导致TypeError。联系人没有更新,我们只是尝试使用setTimeout更改消息的状态。
import React, {useState, setTimeout} from 'react';
const Test = () => {
//Setting up message
const [message, setMessage] = useState('')
//Change the state of message to Testing after 6 seconds
setTimeout(() => {setMessage("Testing")}, 6000)
return (
<div></div>
)
}
export default Test
Run Code Online (Sandbox Code Playgroud)
作为一个项目,我正在创建一个学生数据库。但是根据valgrind,我的程序中存在内存泄漏,我不知道为什么。我真的不能说太多:我不明白为什么记忆肯定会丢失。
学生结构:
typedef struct {
char student_number[7];
char *first_name;
char *last_name;
int round_points[6];
} Student;
Run Code Online (Sandbox Code Playgroud)
免责声明:我正在使用gcc选项-std=c99,因此必须实现自己的strdup()。
重要的代码段:
char *copy_string(const char *string) {
int len = strlen(string);
char *copy = calloc(len + 1, sizeof(char));
if (copy == NULL)
return NULL;
strcpy(copy, string);
/* copy[len] = '\0'; */
return copy;
}
char **parse_one_line_params(const char *one_line, int param_count) {
char *copy = copy_string(one_line);
if (copy == NULL)
return NULL;
//copy_start is used to free the copy string in the …Run Code Online (Sandbox Code Playgroud)