How can I pass function with React Hooks? I've used useCallback, but it shows an error that it is not a function.
In class component I was doing it like this:
hidePopUp = (event) => {
event.preventDefault();
this.setState({
popup: !this.state.popup
})
}
Then I was passing function:
<Popup hidePopUp={this.hidePopUp}/>
That's my problem with Hooks:
const hidePopup = useCallback(
(event) => {
event.preventDefault();
setPopup(!popup);
}, []
);
<Popup hidePopup={() => hidePopup}/>
And this is my button in Popup component:
<button onClick={(event)=>this.props.hidePopUp(event)}></button>
hidePopUpprop to a function that takes a single argument. In your second example, you're setting thehidePopUpprop to a function that takes no arguments, and returns a function. Have you tried<Popup hidePopup={hidePopup} />?hidePopup, function name -hidePopUp. 'Up' - from the upper case