How to use const in ReactJS

时间:2015-07-31 20:03:05

标签: javascript reactjs const

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?

1 个答案:

答案 0 :(得分: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' }
];

//然后你可以像这样导入它

import menuItems from "./menuItems";