pmiranda
pmiranda

Reputation: 8420

React, read const from another component

I have this 2 components:

BasicMaintainer

// etc...
import GeneratingUnit from "./generating-unit";

class BasicMaintainer extends Component {
  constructor(props) {
    super(props);
    // etc...
  }

  // etc...

  handleOnSelectFile = event => {
    here I want to use: GeneratingUnit.columns.length
  }

The other component GeneratingUnitis:

import React from 'react';
//...code

export const columns = [
    {name: 'ID', numeric: false, disablePadding: true, key: 'id'},
    {name: 'NOMBRE', numeric: false, disablePadding: true, key: 'nombre'},
    {name: 'DESCRIPCIÓN', numeric: false, disablePadding: true, key: 'descripcion'},
    {name: 'IT UNIDAD GENERADORA', numeric: false, disablePadding: true, key: 'it_unidad_generadora'},
]

const GeneratingUnit = props => <div>
    //code...
</div>

export default GeneratingUnit;

How can I use const columns in handleOnSelectFile of BasicMaintainer component?

Upvotes: 0

Views: 168

Answers (2)

Dmitry Sobolevsky
Dmitry Sobolevsky

Reputation: 1191

If you don't modify this variable: remove columns variable from component and put in the current module in the shared folder with global variables and functions, and import in your components.

folder: shared
file: index.js

index.js

export const columns = [
    {name: 'ID', numeric: false, disablePadding: true, key: 'id'},
    {name: 'NOMBRE', numeric: false, disablePadding: true, key: 'nombre'},
    {name: 'DESCRIPCIÓN', numeric: false, disablePadding: true, key: 'descripcion'},
    {name: 'IT UNIDAD GENERADORA', numeric: false, disablePadding: true, key: 'it_unidad_generadora'},
]

in components:

import { columns } from "./shared";

Upvotes: 0

Ali Mousavi
Ali Mousavi

Reputation: 915

Import it in your BasicMaintainer file:

import GeneratingUnit, {columns} from "./generating-unit";

Upvotes: 3

Related Questions