Anuj Dahiya
Anuj Dahiya

Reputation: 461

const can only be used in a .ts file react native

const ACCESS_TOKEN = 'access_token';

react-native-cli: 2.0.1
react-native: 0.47.2

I am watching a tutorial video in which the expert get the value from the api and stored it inside the const ACCESS_TOKEN but when i am doing it in my code it gives me an error

const can only be used in a .ts file react native

Please suggest.

Upvotes: 42

Views: 59443

Answers (6)

Rafia Zafar
Rafia Zafar

Reputation: 412

Try using this.state in your constructor of class component in react.js file. Assign a property to this.statement and it will not give you any error. For example:

class App extends React.Component {
constructor(){
    super()
    this.state= {
         anyProperty : itsValue;
    }

}

Upvotes: 0

vishal
vishal

Reputation: 259

This error occurs when you use const, let, var keywords inside a class in javascript. To set a property to a class, you do not use const, let or var keywords.

But you need to use const, var or let keywords to define variables inside a method of a class example

class List extends React.Component{
     // But using const outside method and inside a class will give error
     /* const accessToken = ACCESS_TOKEN  is wrong syntax */
   render() {
     const accessToken = ACCESS_TOKEN;   // will not give error

}

Upvotes: 20

Adi Elisha
Adi Elisha

Reputation: 180

had same problem, for me the slution was that I forgot to close a '}'.

Upvotes: 0

Ashutosh Dash
Ashutosh Dash

Reputation: 610

The const is inside a class, move it outside of the class and the error will disappear.

Upvotes: 6

pashute
pashute

Reputation: 4053

The const is inside a class. Move it outside the class and all will be ok.

Upvotes: 91

Dragomir Kolev
Dragomir Kolev

Reputation: 1108

In your SignInScreen.js file do:

export const ACCESS_TOKEN = "token";

In your other js file you want to use it you will have to do something like this:

import * as globalConst from 'path/to/SignInScreen';

var token = globalConst.ACCESS_TOKEN;

Hope this helps.

Upvotes: 2

Related Questions