DEV Community

Discussion on: React useEffect Hook usages you must know

Collapse
 
marklai1998 profile image
Mark Lai • Edited

One thing I need to point out is useEffect will always run on mount
So Side Effect Runs After X Value Change is kinda misleading

One issue you might face is to make some custom component with 2 way binding with some form lib(like react hook form

const MyCustomeInput = ({onChange, value})=>{
  const [tmpValue,setTmpValue] = useState(value)
  useEffect(()=>{
    onChange(tmpValue)
  },[tmpValue])
  return // some custom Input
}
Enter fullscreen mode Exit fullscreen mode

You'll found that the input field is always dirty in the form state
Its because the onChange will not only fire on value change but also on mount

Collapse
 
atapas profile image
Tapas Adhikary

Thanks, Mark ✌