You have two options: use the object or use the class name, start with the object
class B {
abc() {
alert("Hello World");
}
}
const b = new B();
export default b;
Therefore, when you call this file, you can access the abc function, as shown below.
import b from './B.js';
class A extends Component {
_onItemPressed(item){
b.abc();
}
...
,
class B{}
B.abc = function(){
alert("Hello World");
}
module.exports = {
functions: B
};
, , abc, .
import b from './B.js';
class A extends Component {
_onItemPressed(item){
b.functions.abc();
}
...
. B , .
singleton,
React native -
UPDATE. , :
export default class B extends Component {
constructor(props) {
super(props);
this.abc = this.abc.bind(this);
}
abc(){
alert('Hello World');
}
render() {
return null
}
}
A B
import B from "./B.js";
class A extends Component {
_onItemPressed(item) {
this._b.abc();
}
render() {
return (
<TouchableHighlight
underlayColor={Colors.colors.lightgrey}
style={{ padding: 15 }}
onPress={this._onItemPressed.bind(this)}
>
<Text>Click Me !</Text>
<B ref={ref => (this._b = ref)} />
</TouchableHighlight>
);
}
}