[英]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:) PS:我是初学者:)
if this variable is defined inside your the function of your component you can't export it如果此变量是在组件的 function 中定义的,则无法导出它
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.如果你想将变量传递给其他组件,你可以使用 props、context、redux.... 但它是更高级的主题(redux 和 context)和工具,应该小心操作和使用。 You probably juste need state and props at your level.
您可能只需要 state 和您级别的道具。
Good luck with you journey into React祝你进入 React 之旅好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.