我想做React对onClick
、onChange
等所做的事情.
React可以:
onClick={(event)=>console.log(event)}
我想做:
someProp={(wanna_do_this)=>console.log(wanna_do_this)}
我想做React对onClick
、onChange
等所做的事情.
React可以:
onClick={(event)=>console.log(event)}
我想做:
someProp={(wanna_do_this)=>console.log(wanna_do_this)}
您只需创建一个按您想要的命名的props 并传递它.例如:
import PropTypes from 'prop-types';
function CalledComponent ({onCustom}){
// Here you'll be able to call this custom event/function.
// Let's say I call it in useEffect.
useEffect(onCustom, []);
// Or maybe I call it in many events at the same time.
return (<div onClick={(e)=>onCustom(e)} onHover={(e)=>onCustom(e)}>
I'm called and I execute custom events when I'm clicked or hovered :D
</div>);
}
// PropTypes stuff.
CalledComponent.propTypes = {
onCustom: PropTypes.func,
};
所以稍后我可以简单地调用它并向它传递一个函数.
function ComponentThatCalls(){
return (<CalledComponent onCustom={(e)=>console.log(e.target.value)}/>);
}