Hruday Tamboli
Hruday Tamboli

Reputation: 9

How to export a variable to another react functional component?

I am creating this react app in which I want to export a variable to another react functional component. I am also facing an issue since when a user clicks on a button, only then will the variable contain value or else it is undefined and exporting an undefined variable is not possible. So I was hoping that there is a way I can export the variable only when it contains a value. Thanks in advance to anyone who has helped!

PS: I am a Beginner :)

Upvotes: 0

Views: 6466

Answers (1)

Johan Petrikovsky
Johan Petrikovsky

Reputation: 130

if this variable is defined inside your the function of your component you can't export it


const canExport = 'yes we can'

const App = () => {
    const iAmImpossibleToExport = []

    return <div>The application</div>
}
export { canExport }

export default App 

If you want to pass variables to other components, you can use props, context, redux.... but it's more advanced topics (redux and context) and tools that should be manipulated and used carefully. You probably juste need state and props at your level.

Good luck with you journey into React

Upvotes: 2

Related Questions