0

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 :)

1
  • Please provide enough code so others can better understand or reproduce the problem. Commented Feb 13, 2022 at 14:53

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.