简体   繁体   中英

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?

// 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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM