Non
Non

Reputation: 8589

How to use const in ReactJS

I have an array that I need to use twice and I don't want to repeat it in my code

const menuItems = [
  { route : 'home', text : 'Game Info' },
  { route : 'players-info', text : 'Players Info' },
  { route : 'money', text : 'Money' },
  { route : 'refunds', text : 'Refounds' },
  { route : 'videos', text : 'Videos' },
  { route : 'tips', text : 'Tips' }
];

and in the class I am doing

render () {
  return <LeftNav
    menuItems={menuItems} />
}

so, lets say that in another file, I want to use that same const menuItems, and render it like this

  render () {

    let tabs = menuItems.map((item) => {
      return <Tab        
        key={item.route}
        label={item.text}
        route={item.route}
        onActive={this._onActive} />
    });

    return <Tabs
        initialSelectedIndex={0}>{tabs}
      </Tabs>;
  }

So, what should I do to use the const across different files?

Upvotes: 0

Views: 1407

Answers (1)

deowk
deowk

Reputation: 4318

// menuItems.js

export default const menuItems = [
  { route : 'home', text : 'Game Info' },
  { route : 'players-info', text : 'Players Info' },
  { route : 'money', text : 'Money' },
  { route : 'refunds', text : 'Refounds' },
  { route : 'videos', text : 'Videos' },
  { route : 'tips', text : 'Tips' }
];

// then you can import it like so

import menuItems from "./menuItems";

Upvotes: 2

Related Questions