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?
答案 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";