react Hooks 封装 useState

import { useState } from "react"

/**
 * 使用表单
 * @param props 
 */
export default function useForm(initialState: any) {

  const [state, setState] = useState(initialState);

  function setFormState(...key: any) {
    console.log(key,'...val...val')
    setState(currentState => {
      var newState = { ...currentState };
      for (var m = 0; m < key.length; m = m + 2) {
        newState[key[m]] = key[m + 1];
      }
      return newState
    })
  }
  return [state, setFormState, setState];
}

  //文件中引入

import useForm from '@/components/ospmodel/useForm'
       //使用 
const [lhys, setLhys] = useForm({//
    startTime: new Date(),//开始时间
    F_NOTE: '',//节点名称
    currentNodeData: null,
  })
 
 
修改
 
 
setLhys("startTime", new Date());
posted @ 2021-10-19 14:51  吃鸡小能手  阅读(249)  评论(0编辑  收藏  举报