编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

前端开发React18 - 表单元素受控,useRef,UUID,DAYJS

wxchong 2024-09-07 01:23:20 开源技术 9 ℃ 0 评论

受控表单绑定:

  • 使用组件的 state (useState) 绑定表单的 value
  • 表单的 value 新值设置 state

import { useState } from 'react'

function App() {

const [num, setNum] = useState('')

return (

<div className="App">

{num}

<br/>

<input value={num} onChange={(e)=>{setNum(e.target.value)}} />

</div>

);

}

export default App;

useRef获取DOM元素:

import { useState, useRef } from 'react'

function App() {

const inpRef = useRef(null)

const [num, setNum] = useState('')

return (

<div className="App">

<input ref={inpRef} value={num} onChange={(e)=>{setNum(e.target.value)}} />

<br/>

<button onClick={(e)=>{ setNum(''); inpRef.current.focus();}}>click clear</button>

</div>

);

}

UUID 插件用法:

dayjs 插件用法:

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表