In my app.js inside the app class there is a variable that I want to export to another module. I tried many options, but unfortunately, the understanding of how to do it correctly did not come. Tell me, please, how to do it right? Export default is already in use in my class
export class App extends Component {
static propTypes = {
//somecode
};
constructor(...args: any): void {
super(...args);
}
render() {
// console.log('app render start');
const test = true;
return (
//somecode
);
}
componentDidMount() {
//somecode
}
componentDidUpdate(prevProps): void {
//somecode
}
}
In the example above, i need to export variable 'test'
I would be grateful for any help.
If you are exporting a variable (or an arrow function) as a default export, you have to declare it on 1 line and export it on the next. You can't declare and default export a variable on the same line.
In React we use the keyword export to export a particular module or a named parameter or a combination. Let us now see the different ways we can use the import operation in React. Exporting default export: We have already learned that every module is said to have at most one default export.
Assuming your intention with test
is to define something like a "project constant" (which is what I gather, from your use of the const
keyword), then you could simply declare test
outside of the App
class, and then export it from the module in the same way you are exporting the class:
/*
Export test variable
*/
export const test = true;
/*
Export your App class
*/
export class App extends Component {
static propTypes = {
//somecode
};
constructor(...args: any): void {
super(...args);
}
render() {
/*
Access test variable in "global scope" inside render method
*/
console.log(`test is: ${ test }`);
// console.log('app render start');
// const test = true;
return (
//somecode
);
}
componentDidMount() {
//somecode
}
componentDidUpdate(prevProps): void {
//somecode
}
}
And then, from another module in your project you could access test
like so:
import { App, test, } from 'path/to/App';
console.log(`test is: ${ test }`);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With