如何将打字稿代码转换为React

时间:2019-06-21 15:14:03

标签: reactjs typescript

我正在用React应用程序实现用Angular编写的Andreas Aeschlimann's Gabor过滤器。我需要将一些Typescript函数调用转换为React jsx。这是原始的TS代码:

  var element = document.createElement('a'); //created the anchor element 
  element.setAttribute('href', url); //i know the url, hence setting the href
  element.setAttribute('download', 'dnpdf');  //setting the name of the downloaded file

  element.style.display = 'none';
  document.body.appendChild(element);

  element.click();

  document.body.removeChild(element);

我的jsx实现是:

async gaborConvolution2(f: Float32Array,
                       xi: number,
                       sigma: number,
                       lambda: number,
                       theta: number,
                       amount: number,
                       successCallback: (fConv: Float32Array, event: MessageEvent) => void,
                       errorCallback: (event: ErrorEvent) => void) {

    // Create a new worker
    const backgroundWorker: Worker = new Worker("assets/js/gaborConvolution2.js");

    // The success callback
    backgroundWorker.onmessage = (event: MessageEvent) => {
        backgroundWorker.terminate();
        successCallback(event.data.fConv, event);
    };

    // The error callback
    backgroundWorker.onerror = (event: ErrorEvent) => {
        backgroundWorker.terminate();
        errorCallback(event);
    };

    // Post the data
    backgroundWorker.postMessage({f: f, xi: xi, sigma: sigma, lambda: lambda, theta: theta, amount: amount});

}

jsx中的async gaborConvolution2(f, xi, sigma, lambda, theta, amount, successCallback(fConv, event) => void, errorCallback(event) => void) { // Create a new worker const backgroundWorker = new Worker("assets/js/gaborConvolution2.js"); // The success callback backgroundWorker.onmessage = (event) => { backgroundWorker.terminate(); successCallback(event.data.fConv, event); }; // The error callback backgroundWorker.onerror = (event) => { backgroundWorker.terminate(); errorCallback(event); }; // Post the data backgroundWorker.postMessage({f: f, xi: xi, sigma: sigma, lambda: lambda, theta: theta, amount: amount}); } } successCallback(fConv, event) => void,行不正确。有人可以告诉我怎么了吗?

1 个答案:

答案 0 :(得分:0)

如果我正确地理解了您的问题,则只需要声明要传递给gaborConvolution2的每个函数的默认值,以防万一您没有收到该函数。像这样:

gaborConvolution2(f,
        xi,
        sigma,
        lambda,
        theta,
        amount,
        successCallback = () => {},
        errorCallback = () => {}, ){
        //do something, call functions, etc...
        }