我如何通过react-hooks将函数应用于值

我是React Hooks的初学者,我在React中有一个功能组件:

import React, {useState} from 'react'

 const myFunction() {
    const [myValue, setValue] = useState(10000)

function currencyFormat(num) {
   return parseFloat(num).toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,')
}

handleChange = (e) => {
...
}       

 return(
      <input value={myValue} onChange={handleChange}
    )
 }

考虑到React Hooks的良好实践,如何将currencyFormat应用于myValue。

I got currencyFormat from here

评论
  • 小、白痴
    小、白痴 回复

    we handle it the same way in class-based components; but with hooks instead of this.setState({someState}) we hand to related hook; and there is no hook specific thing to consider for this case; so your handle change will be like this

    handleChange = (e) => {
      setValue(currencyFormat(e.target.value))
    }   
    

    编辑: 初始值

    const [myValue, setValue] = useState(currencyFormat(10000))