ant-design form initialValues赋值state的坑

项目中需要将Form表单赋值初始值以供修改,但是我用useState设置了选中的值然后initialValues赋值却发现,点击出来显示的始终是上一次点击的值,然后用p标签重新显示这个state,发现又是最新的。

用useEffect也不起作用。我就放弃了使用initialValues,然后使用form.setFieldsValue()直接给form设置值。这下就行了

复制代码
   <ModalForm
        title="修改日程"
        open={modalVisit1}
        onFinish={async (values: any) => {
          console.log(values);
          const params={
            ...values,
            id: id
          }
          const res=await scheduleService.updateSchedule(params)
          if(res.success){
            message.success('修改成功'); // 提示用户提交成功
            const res=await scheduleService.getScheduleList()
            console.log(res.data);
            setMatchList(res.data)
          }
          return true;
        }}
      onOpenChange={setModalVisit1}
      // initialValues={updateRecord}
        form={form1}
        width={'40%'}
        modalProps={{
          destroyOnClose: true,
        }}
const [form1] = Form.useForm();

 form1.setFieldsValue(res.data)

React hooks 不能拿到最新的的setState的值

复制代码
  useEffect(() => {
    setUpdateRecord(updateRecord)
    console.log(updateRecord);
  }, [updateRecord]);
相关推荐
excel1 分钟前
Vue 模板编译中的资源路径转换:transformSrcset 深度解析
前端
excel9 分钟前
Vue 工具函数源码解析:URL 解析与分类逻辑详解
前端
excel11 分钟前
Vue SFC 样式预处理器(Style Preprocessor)源码解析
前端
excel13 分钟前
深度解析:Vue Scoped 样式编译原理 —— vue-sfc-scoped 插件源码详解
前端
excel15 分钟前
Vue SFC Trim 插件源码解析:自动清理多余空白的 PostCSS 实现
前端
excel18 分钟前
Vue SFC 样式变量机制源码深度解析:cssVarsPlugin 与编译流程
前端
excel21 分钟前
🧩 Vue 编译工具中的实用函数模块解析
前端
excel24 分钟前
🧩 深入剖析 Vue 编译器中的 TypeScript 类型系统(第五篇)
前端
excel31 分钟前
🧩 深入剖析 Vue 编译器中的 TypeScript 类型系统(第六篇 · 终篇)
前端
不吃香菜的猪33 分钟前
el-upload实现文件上传预览
前端·javascript·vue.js