简体   繁体   English

如何将变量导出到另一个反应功能组件?

[英]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.

相关问题 如何使用功能组件导出 React 图标? - How to Export React Icons Using Functional Component? 如何从一个反应功能组件调用功能方法到另一个反应功能组件? - How to call a functional method from one react functional component to another react functional component? 如何将 state 导出到 React 中的父功能组件? - How can I export state to parent functional component in React? 如何在单击一次按钮时呈现另一个反应组件? (功能组件) - How to render another react component on a single button click? (Functional Component) 如何从反应组件导出变量? - How to export variable from react component? 如何通过 React Router Link 将值从功能组件传递到另一个功能组件? - How can I pass values from a functional component through a React Router Link to another functional component? 如何重写将另一个类扩展为功能组件的 React 类? - How to rewrite a React class that extends another class as a functional component? 如何在 React 中将数组作为结果从一个功能组件传递到另一个功能组件 - How to pass array as result from one functional component to another in React 如何在反应功能组件中为按钮变量切换 className? - How do I toggle className for button variable in react functional component? 如何在功能性 React 组件中修改 function 调用中的变量 - How to modify variable in function call in functional React component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM