Dr. Rajkiran Jogu
Dr. Rajkiran Jogu

Reputation: 75

How to access a variable defined in a react component from another js file?

How to access a variable defined in a react component from another js file?

I have a react component like follows:

App.js

class App extends React.Component {
   constructor(props) {
       super(props);
       this.name = "Rajkiran";
    }
}

I have a utils file like below

utils.js

export fun1() {
    //I want to access the "name"  value defined in App.js here
}
export fun2() {

}

Upvotes: 0

Views: 220

Answers (1)

Alexander
Alexander

Reputation: 1380

You can add the name to the function argument. And when you call the fun1 pass this.name to function.

export fun1(name){
    //I want to access the "name"  value defined in App.js here
}

// somewhere in App.js
fun1(this.name)

Upvotes: 1

Related Questions