专业的编程技术博客社区

网站首页 > 博客文章 正文

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

baijin 2024-08-12 13:59:58 博客文章 7 ℃ 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:

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

欢迎 发表评论:

最近发表
标签列表