Reactjs 函数是否有didmount方法?

Reactjs 函数是否有didmount方法?,reactjs,Reactjs,我找了很多,但从来没有找到答案。 我有一个返回一些React.Component的函数(不是类),我喜欢使用它,因为它很容易使用状态。通常我使用类,但在某些地方使用函数感觉更好 有没有办法检查函数是否被挂载? 我不想造成任何内存泄漏 示例函数: export default function foo() { const [bar, setBar] = React.useState(false) function looping() { <some code

我找了很多,但从来没有找到答案。 我有一个返回一些React.Component的函数(不是类),我喜欢使用它,因为它很容易使用状态。通常我使用类,但在某些地方使用函数感觉更好

有没有办法检查函数是否被挂载? 我不想造成任何内存泄漏

示例函数:

export default function foo() {
    const [bar, setBar] = React.useState(false)
    function looping() {
        <some code with useState>
    }
    return (
        <div>Something</div>
    )
}
导出默认函数foo(){
常量[bar,setBar]=React.useState(false)
函数循环(){
}
返回(
某物
)
}
您可以使用钩子:

导出默认函数foo(){
常量[bar,setBar]=React.useState(false)
useffect(()=>{
console.log('mounted')
return()=>{
console.log('unmounted')
}
}, [])
函数循环(){
}
报税表(
某物
)
}
还有一个同步工作的

您可以使用钩子:

导出默认函数foo(){
常量[bar,setBar]=React.useState(false)
useffect(()=>{
console.log('mounted')
return()=>{
console.log('unmounted')
}
}, [])
函数循环(){
}
报税表(
某物
)
}

还有一个同步工作的

多么美丽和容易,从来没有找到这样的方式。非常感谢,我会尽快接受这个答案。多么美丽和简单,从来没有找到过这种方式。非常感谢,我会尽快接受这个答复。
export default function foo() {
  const [bar, setBar] = React.useState(false)

  useEffect(() => {
    console.log('mounted')
    return () => {
      console.log('unmounted')
    }
  }, [])

  function looping() {
    <some code with useState >
  }
  return ( 
  <div>Something </div>
  )
}