How to call a function from another class in React-Native?

I am working on React-Native, I want to call a function from another class, but when I try to do this, some error appears.

Class A

import B from './B.js';

class A extends Component {
    _onItemPressed(item){
        B.abc();
    }

    render() {
      return (
         <TouchableHighlight
            underlayColor={Colors.colors.lightgrey}
            style={{padding: 15}}
            onPress={this._onItemPressed.bind(this)}>
         <Text>Click Me !</Text>
         </TouchableHighlight>
      );
    }
}

Class B

class B extends Component {

    abc(){
      alert('Hello World');
    }

    render() {
      return (
         <View>
            <Text>Welcome to React Native</Text>
         </View>
      );
    }
}

But the error message comes after clicking the button in class A, undefined is not a function (the rating is "B.default._abc ()") '

Please kindly read my post and offer me some solution.

thank

+9
source share
8 answers

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>
    );
  }
}
+7

, B.abc() new B().abc();

+3

, B.

class B extends Component {
   static abc(){
     alert('Hello World');
   }}
export default B

A

import B from './B';

, .

+2

, - , A,

export default class B extends Component {
    constructor(props){
      super(props);
    }
    abc(){
        alert('Hello World');
    }

    render() {
        return (
            <View>
                <Text>Welcome to React Native</Text>
            </View>
        );
    }
}






import B from './B.js';

export default class A extends B {

    _onItemPressed(item){
        this.abc();
    }

    render() {
        return (
            <TouchableHighlight
                underlayColor={Colors.colors.lightgrey}
                style={{padding: 15}}
                onPress={this._onItemPressed.bind(this)}>
                <Text>Click Me !</Text>
            </TouchableHighlight>
        );
    }
}
+1

, B, . export B : export default B.

,

0

abc static B.

export default class B extends Component {

    static abc(){
      alert('Hello World');
    }
}
0

Import the first class into another class where you want to use the function defined in the first class. In this case, we use a function defined from class1 to class2.

export default class class1 extends React.Component{
constructor(props)
{
...
}

static function(){
...
}
}


**Now import it to another class i:e; class2**

import class1 from 'class1';

export default class class2 extends React.Component{

componentWillMount()
{
class1.function();
}



}
0
source

I tried various solutions on this page, but it did not work. I am copying the solution from another webpage here. Very simple and impressive. May help someone looking for a simple solution:

How to call another class function from a default class in React Native

Here is the complete code example:

import { StyleSheet, View, Alert, Platform, Button } from 'react-native';

export default class MyProject extends Component {

  constructor(props) {

       super(props)

      Obj = new Second();

     }

     CallFunction_1=()=>{

      Obj.SecondClassFunction() ;

     }

     CallFunction_2=()=>{

            Obj.SecondClassFunctionWithArgument("Hello Text");

    }

  render() {

    return (

      <View style={styles.MainContainer}>

        <View style={{margin: 10}}>

          <Button title="Call Another Class Function Without Argument" onPress={this.CallFunction_1} />

        </View>

        <View style={{margin: 10}}>

          <Button title="Call Another Class Function With Argument" onPress={this.CallFunction_2} />

        </View>

      </View>

    );
  }
}

class Second extends Component {

  SecondClassFunction=()=>{

    Alert.alert("Second Class Function Without Argument Called");

  }

  SecondClassFunctionWithArgument=(Value)=>{

    Alert.alert(Value);

  }

}

const styles = StyleSheet.create(
{
  MainContainer: {
    justifyContent: 'center',
    alignItems: 'center',
    flex: 1,
    backgroundColor: '#F5FCFF',
    paddingTop: (Platform.OS) === 'ios' ? 20 : 0
  }

});
0
source

Source: https://habr.com/ru/post/1675760/


All Articles