简体   繁体   中英

Export and Import Functions in React Component

I'm creating a webapp that uses react. I have a main App.js file that serves as the main component. I then have a subcomponent that has functions that create buttons. For example, I have a renderLogoutButton() that contains a logo, text, etc.

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?

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.

This will also shrink the size and complexity of your App component, which is preferable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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