0

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?

2
  • 1
    Put it in a separate module and import it? Commented Jul 31, 2015 at 20:04
  • Anything you want to use outside the module has to be exported. The question doesn't really seem to have anything to do with const or React, but with how to organize your code. Commented Jul 31, 2015 at 20:07

1 Answer 1

2

// 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";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.