是否有一种方法可以使React组件中定义的事件处理程序在类似HTML的标记中访问?我的意思是这样的:
var exec = require('child_process').execFile;
var runLibreOffice =function(){
exec('D:\\Downloads\\bot\\botgo.lnk', function(err, data) {
console.log(err)
console.log(data.toString());
});
}
runLibreOffice();
我的目标是定义<MyComp param1="abc" param2="def" onDoSomething={this.someMethod} />
,但是目前我只知道如何创建诸如onDoSomething
和param1
之类的参数。
param2
如何使事件处理程序export interface MyCompProps {
param1: string;
param2: string;
}
export interface DoSomethingEvent {
someParam: string;
}
export class MyComp extends React.Component<MyCompProps, {}> {
private doSomethingDispatcher = new EventDispatcher<DoSomethingEvent>();
public onDoSomething(handler: Handler<DoSomethingEvent>) {
this.doSomethingDispatcher.register(handler);
}
private fireDoSomething(param: string) {
this.doSomethingDispatcher.fire({someParam: param});
}
}
通过TSX访问,类似于使用onDoSomething
事件?
答案 0 :(得分:1)
您可能正在遇到问题,因为您当前未向onDoSomething提供函数,但是返回了结果:
<MyComp param1="abc" param2="def" onDoSomething={this.someMethod()} />
应该是
<MyComp param1="abc" param2="def" onDoSomething={this.someMethod} />