I Get "no Such Method" When Using Es6 Static Functions
Solution 1:
Good thing you got things working, but I'd just like to add a solution that more closely resembles what you were originally trying to do, and heeds the points made in the SO question you linked.
There is just no need to use class
to export bunch of static methods. I don't see how it would add functionality, ease of use or clarity to the code. Quite the contrary actually, the syntax looks more verbose than exporting plain old ES5 style objects and even more so when we bring ES6+ sweetness to the mix.
Your original example of the utils module works just fine. The problem is in the import.
As you are just exporting one default object, the correct way to import it would be
importUtilsfrom'./utils/utils';
No brackets, no asterisks, just a name for the imported default object, which can then be used like var text = Utils.textFormat(...)
.
We can go further, though. By ditching the whole "export one default object" approach, we can use the full power of the module system.
utils.js:
'use strict'functiontextFormat(args) {
var s = args[0];
...
return s;
}
constsomeOtherUtility = str => str + ' works!';
export { textFormat, someOtherUtility };
Now the exported functions can be used like this
import * asUtilsfrom'./utils/utils';
var text = Utils.textFormat(rowData.text, "<Text style={{color:'blue'}}>Hey there</Text>");
Or like this
import {textFormat} from'./utils/utils';
var text = textFormat(rowData.text, "<Text style={{color:'blue'}}>Hey there</Text>");
Whichever you prefer.
Solution 2:
You are mixing the two approaches laid out in the linked question, which doesn't work. It should either be
// utils.jsexportdefault {
textFormat(args) {
var s = args[0];
for (var i = 0; i < args.length - 1; i++) {
var reg = newRegExp("\\{" + i + "\\}", "gm");
s = s.replace(reg, args[i + 1]);
}
return s;
}
}
// main fileimportUtilsfrom'./utils/utils';
…
var text = Utils.textFormat(rowData.text, "<Text style={{color:'blue'}}>Hey there</Text>");
or
// utils.jsexportfunctiontextFormat(args) {
var s = args[0];
for (var i = 0; i < args.length - 1; i++) {
var reg = newRegExp("\\{" + i + "\\}", "gm");
s = s.replace(reg, args[i + 1]);
}
return s;
}
// main fileimport * asUtilsfrom'./utils/utils';
…
var text = Utils.textFormat(rowData.text, "<Text style={{color:'blue'}}>Hey there</Text>");
(the latter is preferable)
Post a Comment for "I Get "no Such Method" When Using Es6 Static Functions"