在这段代码中 可能我正在重置此表单,所有值都会重置,但
swt_culture_type
的值不会重置。
const { data: GetCultureTestHeadersDetails } = useQuery({
queryKey: ["headerDataDetailsKey", props?.culture_test_header_id, props?.test_component_id],
queryFn: async () => {
return await GetCultureTestHeadersDetailsEP(props?.culture_test_header_id, props?.test_component_id, true, 5, 1);
},
});
React.useEffect(() => {
if (GetCultureTestHeadersDetails?.data?.culture_type) {
reset({
txt_code: GetCultureTestHeadersDetails.data.culture_code,
atc_test_component: {
test_component_id: GetCultureTestHeadersDetails.data.test_component_id,
test_component_code: GetCultureTestHeadersDetails.data.test_component_print_name,
test_component_print_name: GetCultureTestHeadersDetails.data.test_component_print_name,
item_id: 0,
},
swt_is_colony_count: GetCultureTestHeadersDetails.data.is_colony_count,
txt_colony_count_label: GetCultureTestHeadersDetails.data.colony_count_label,
txt_colony_count_default_value: GetCultureTestHeadersDetails.data.colony_count_default_value,
swt_is_gram_strain: GetCultureTestHeadersDetails.data.is_gram_strain,
txt_gram_strain_label: GetCultureTestHeadersDetails.data.gram_strain_label,
ddl_gram_stain_default_value: GetCultureTestHeadersDetails.data.gram_strain_default_value,
swt_is_organism: GetCultureTestHeadersDetails.data.is_organism_isolated,
txt_organism_isolated_label: GetCultureTestHeadersDetails.data.organism_isolated_label,
ddl_organism_default_value: GetCultureTestHeadersDetails.data.organism_isolated_default_value,
swt_is_solid_media: GetCultureTestHeadersDetails.data.is_solid_media,
ddl_solid_media_default_value: GetCultureTestHeadersDetails.data.solid_media_default_value,
txt_solid_media_label: GetCultureTestHeadersDetails.data.solid_media_label,
swt_is_liquid_media: GetCultureTestHeadersDetails.data.is_liquid_media,
ddl_liquid_media_default_value: GetCultureTestHeadersDetails.data.liquid_media_default_value,
txt_liquid_media_label: GetCultureTestHeadersDetails.data.liquid_media_label,
swt_is_report_type: GetCultureTestHeadersDetails.data.is_report_type,
report_type_default_value: GetCultureTestHeadersDetails.data.report_type_default_value,
txt_report_type_label: GetCultureTestHeadersDetails.data.report_type_label,
txt_special_instructions: GetCultureTestHeadersDetails.data.special_instruction,
txt_description: GetCultureTestHeadersDetails.data.description,
ddl_max_count: Number(GetCultureTestHeadersDetails.data.max_result_count),
swt_culture_type: GetCultureTestHeadersDetails?.data?.culture_type,
swt_status: GetCultureTestHeadersDetails.data.status,
});
setValue("swt_culture_type", GetCultureTestHeadersDetails.data.culture_type);
console.log("swt_culture_type:", GetCultureTestHeadersDetails.data.culture_type);
}
}, [GetCultureTestHeadersDetails, GetCultureTestHeadersDetails?.data]);
我直接设置值但仍然总是显示true
所有值都被重置,但 swt_culture_type 的值没有重置。这是因为您将值记录在设置值的同一块内,导致记录的值看起来没有变化。尝试记录同一 useEffect 之外的值以查看更新后的值。
通过将日志记录分离到不同的 useEffect 中,您可以确保日志语句在重置后捕获更新的状态。