2017-09-24 83 views
0

我想我可以做這樣的事情:如何從JavaScript的另一個文件導入數據/反應

export default() => { 
    return [ 
     { 
      text: 'Full-time', 
      value: 'fulltime', 
      key: 'fulltime' 
     }, 
     { 
      text: 'Part-time', 
      value: 'parttime', 
      key: 'parttime', 
     }, 
     { 
      text: 'Freelance', 
      value: 'freelance', 
      key: 'freelance', 
     }, 
    ] 
}; 

然後在我的部分,我可以得到這些數據在這樣下拉使用:

import { positionTypeOptions } from '../components/data/PositionTypes'; 

<Form.Select label="&nbsp;" placeholder="Type" options={positionTypeOptions} width={3} /> 

的數據似乎但是並沒有出口。數據未定義。任何想法如何做到這一點?我想返回一個數組在另一個組件中使用。

+0

你是什麼意思似乎沒有出口?你有什麼錯誤嗎?什麼positionTypeOptions評估? – lilezek

+0

不好意思在{}中更新了帶有positionTypeOptions的問題,該問題返回undefined。當我這樣做沒有{}它返回一個函數,我想要一個數組 – user1009698

回答

2

您正在導出一個匿名函數作爲默認值。試試這個:

export const positionTypeOptions = [ 
    { 
     text: 'Full-time', 
     value: 'fulltime', 
     key: 'fulltime' 
    }, 
    { 
     text: 'Part-time', 
     value: 'parttime', 
     key: 'parttime', 
    }, 
    { 
     text: 'Freelance', 
     value: 'freelance', 
     key: 'freelance', 
    }, 
]; 
0

你不需要從導出中返回一個函數。只需返回一個object

export default { 
    [ 
     { 
      text: 'Full-time', 
      value: 'fulltime', 
      key: 'fulltime' 
     }, 
     { 
      text: 'Part-time', 
      value: 'parttime', 
      key: 'parttime', 
     }, 
     { 
      text: 'Freelance', 
      value: 'freelance', 
      key: 'freelance', 
     }, 
    ] 
}; 

和進口只是

import positionTypeOptions from '../components/data/PositionTypes'; // remove the curly brackets as you have a default export. 
相關問題