How to disable a button when an input is empty?

前端 未结 4 1648
心在旅途
心在旅途 2020-12-07 13:09

I\'m new to React. I\'m trying to disable a button when an input field is empty. What is the best approach in React for this?

I\'m doing something like the following:

4条回答
  •  悲哀的现实
    2020-12-07 13:23

    You'll need to keep the current value of the input in state (or pass changes in its value up to a parent via a callback function, or sideways, or such that it eventually gets passed back into your component as a prop) so you can derive the disabled prop for the button.

    Example using state:

    
    
    
    

提交回复
热议问题