其他分享
首页 > 其他分享> > react Hooks 封装 useState

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());

标签:...,Hooks,react,useState,key,newState,useForm,setState
来源: https://www.cnblogs.com/duokexiao/p/15424710.html