【发布时间】:2016-05-23 23:32:54
【问题描述】:
我正在尝试制作一个可重用的 ReactJS 按钮组件,需要帮助 将函数传递给组件,然后将其用作单击事件。按钮的点击事件不起作用。
下面是调用组件的代码:
export var MyPublicFunction = function (inArg: number) {
alert(inArg);
}
ReactDOM.render(<MyButton name="My Button" clickFunction={MyPublicFunction(1)} >Button</MyButton>, document.getElementById('content'));
这里是我要编写的组件:
interface myProps {
name: string;
clickFunction: any
}
class MyButton extends React.Component<myProps, {}> {
constructor(props: myProps) {
super(props);
}
render() {
return (<div>
<button ref="btn1" onClick={this.props.clickFunction} >
{this.props.name}
</button>
</div>);
} //end render.
} //end class.
【问题讨论】:
标签: reactjs typescript