简体   繁体   English

在React组件中导出和导入函数

[英]Export and Import Functions in React Component

I'm creating a webapp that uses react. 我正在创建一个使用react的webapp。 I have a main App.js file that serves as the main component. 我有一个主要的App.js文件,它是主要组件。 I then have a subcomponent that has functions that create buttons. 然后,我有一个具有创建按钮功能的子组件。 For example, I have a renderLogoutButton() that contains a logo, text, etc. 例如,我有一个renderLogoutButton() ,其中包含徽标,文本等。

I need the logout button in a different subcomponent and don't want to have to duplicate the function. 我需要另一个子组件中的注销按钮,并且不想重复该功能。 How can I export the renderLogoutButton() from one React component and import it into another React component? 如何从一个React组件导出renderLogoutButton()并将其导入另一个React组件?

All help is appreciated; 感谢所有帮助; thanks in advance. 提前致谢。 :) :)

If you move your renderLogoutButton code to a seperate LogoutButton component, you can re-use the component wherever you want without code duplication. 如果将renderLogoutButton代码移动到单独的LogoutButton组件,则可以在任何需要的地方重复使用该组件,而无需重复代码。

This will also shrink the size and complexity of your App component, which is preferable. 这还将缩小App组件的大小和复杂性,这是可取的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM