user5553868
user5553868

Reputation:

Reactjs Cant declare and export a constant

I want to export this keys to app.js but cant do it showing this error :-

'const' can only be used in a .ts file.

ApiKey.js

import React from 'react';

const firebaseConfig;
export default class firekeys extends React.Component {
     const firebaseConfig ={
        apiKey: "key",
          authDomain: "auth",
          databaseURL: "url",
          projectId: "Id",
          storageBucket: "",
          messagingSenderId: "SID"
    }
};

App.js

import React from 'react';
import firekeys from "./constants/ApiKeys"
import * as firebase from 'firebase';
firebase.initializeApp(firekeys.firebaseConfig);

Upvotes: 0

Views: 1281

Answers (2)

Andrew
Andrew

Reputation: 28539

You should rewrite ApiKeys.js as follows, unless you have some specific reason for needing them in a component:

export default firebaseConfig = {
  apiKey: "key",
  authDomain: "auth",
  databaseURL: "url",
  projectId: "Id",
  storageBucket: "",
  messagingSenderId: "SID"
}

import firekeys from "./constants/ApiKeys" (to be fair the variable you name them isn't important)

Then you should be able to access the keys as follows

firekeys.apiKey

You should probably take a look at the different ways to use let, var and const https://medium.com/podiihq/javascript-variables-should-you-use-let-var-or-const-394f7645c88f

Upvotes: 0

jmargolisvt
jmargolisvt

Reputation: 6088

Two issues I see here: 1. You can't redeclare a const. 2. Your are exporting your class, not the const. Furthermore, you should be assigning class properties to your React class, not variables. Below should work but you'll need to import firebaseConfig as a named export.

export const firebaseConfig ={
    apiKey: "key",
      authDomain: "auth",
      databaseURL: "url",
      projectId: "Id",
      storageBucket: "",
      messagingSenderId: "SID"
}

export default class firekeys extends React.Component {

};

Upvotes: 2

Related Questions