D
D
Denis2018-07-05 18:10:20
React
Denis, 2018-07-05 18:10:20

How to export properties from a component?

Good evening. Tell me if it is possible to export a property from a React component. For example, I created a component in the home folder. In this folder, in addition to the home.jsx component itself, there is an index.js from which Home is exported. Home has a getAdress() method that returns a string with the address. Can I separately export this method to index.js? The question is specifically focused on whether it is possible to export a component method separately from the component itself!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton Filippov, 2018-07-05
@vicodin

so don't write it in a component,

// Home.jsx
export const getAddress = () => {}
export default const Home = () => {}

// index.js 
import {getAddress}, Home from "./Home"

A
Anton Spirin, 2018-07-05
@rockon404

class ExpampleComponent extends React.Component {
  static exampleStaticMethod() {
    // some stuff without using 'this' keyword
  }
}

export default ExpampleComponent;

import ExpampleComponent from './ExpampleComponent';

ExpampleComponent.exampleStaticMethod();

D
Denis, 2018-07-05
@den0820

Okay, let's say the Home structure is as follows:

import {uniqueId} from 'lodash';

export class Home extends React.Component {
   uid = uniqueId();

   // some code..
}

I want to export uid to index.js

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question