Reputation: 75
How to access a variable defined in a react component from another js file?
I have a react component like follows:
class App extends React.Component {
constructor(props) {
super(props);
this.name = "Rajkiran";
}
}
I have a utils file like below
export fun1() {
//I want to access the "name" value defined in App.js here
}
export fun2() {
}
Upvotes: 0
Views: 220
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