Verex
Verex

Reputation: 25

React Native: Calling a function that's inside a functional component from a class

I'm lost on how functional components and class components interact. How do I call a functional component's function from inside a class?

I'm trying to call Initialize()

App.js:

import Firebase from './components/Firebase';

export default class App extends React.Component {

  componentDidMount() {
  // Call Initialize()
  }    

Firebase.js:

const Firebase = () => {
   
  
  const Initialize = () => {
   // Initialize Firebase
   
      firebase.initializeApp(firebaseConfig);

      console.log("Firebase is initialized");
  }
}

export default Firebase;

Upvotes: 1

Views: 1019

Answers (1)

MadDeveloper
MadDeveloper

Reputation: 1030

I think the confusion is more related to JavaScript than any JavaScript library/framework. You are trying to call a private function, only available in the Firebase closure function. If you want to set available the Initialize function, like a "static" method, declare it as property:

const Firebase = () => {
   ...
};
  
Firebase.Initialize = () => {
   // Initialize Firebase
   firebase.initializeApp(firebaseConfig);

   console.log("Firebase is initialized");
};

export default Firebase;

// and then

export default class App extends React.Component {
  componentDidMount() {
    Firebase.Initialize()
  }
}

Upvotes: 3

Related Questions