I have a pretty big javascript legacy code base of a WebApplication, which includes the presentation/data model/controller/connectivity. I'm trying to replace the presentation layer technology with ReactNative, while keep the existing data model/controller/connectivity parts. So the first question is, after I built the ReactNativeComponent where we have the UI layout defined, binding established, styles created... but I need to call some js functions from the legacy code. How can I require/import an existing js file (not a ReactNativeComponent) to one ReactNativeComponent and use it?
2 Answers
I've had this problem before too. Something I did, unable to find more elegant solutions, was to wrap my legacy code in a module.
Let's say this is foo.js
var Foo = (function() {
var mod = {
init: function() {
// legacy code here...
}
}
return mod;
})()
module.exports = Foo;
then in calling file:
import foo from './somewhere/foo';
foo.init();
I didn't love this approach, but it worked. Hopefully it can somewhat help.