2017-01-09 90 views
-1

什麼原因導致此錯誤?雙RelexDOM雙花括號錯誤

Syntax error: Unexpected token, expected , 
{ Object.keys(form_routes).map((key,index) => <Route path={key} component={{ form_routes[key] }}></Route>) } 

{{ form_routes[key] }}

充分相關的代碼:

const form_routes = { 
    'start': "Start", 
    'part1': "Part1", 
    'part2': "Part2", 
    'part3': "Part3", 
    'part4': "Part4", 
    'part5': "Part5", 
    'part6': "Part6" 
} 

ReactDOM.render(
    <Provider store={store}> 
     <Router history={hashHistory} render={applyMiddleware(useRelativeLinks())}> 
     <Route path="/" component={App}> 
      <IndexRedirect to='/search'/> 
      <Route path='main' component={MainMenu}/> 
      <Route path='search' component={Search}/> 
      <Route path='form' component={Form}> 
       <IndexRedirect to='start'/> 
       { Object.keys(form_routes).map((key,index) => <Route path={key} component={{ form_routes[key] }}></Route>) } 
      </Route> 
     </Route> 
     </Router> 
    </Provider>, 
    root 
); 

作品如果純:

<Route path='start' component={Start}></Route> 
<Route path='part1' component={Part1}></Route> 
<Route path='part2' component={Part2}></Route> 
<Route path='part3' component={Part3}></Route> 
<Route path='part4' component={Part4}></Route> 
<Route path='part5' component={Part5}></Route> 
<Route path='part6' component={Part6}></Route> 

回答

1

component={...}應該包含一個表達式。你給的表達式是{ form_routes[key] },這是一個無效的對象初始值設定項。這會觸發相同的錯誤:

console.log({ form_routes[key] }); 

您可能只是想要component={ form_routes[key] }

+0

當它只是'component = {form_routes [key]}'它不顯示我的組件。我認爲這是因爲它等於'component =「Start」'。其他路由模式不起作用的是'component = {Start}' –

+0

@ A.Lau:當然不會,'form_routes'包含字符串。 '{「Start」}'!='{Start}'。你必須在'form_routes'('Start',而不是'「Start」')中放入等價值,然後'{form_routes [key]}'才能工作。 –

+0

謝謝。這對我來說有點愚蠢。花括號讓我困惑了一下。 –