2017-10-21 51 views
1

如何根據條件以正確的方式發送操作: 我做了以下操作,但出現語法錯誤。如何根據路由器中的條件發送操作


const PrivateRoute = ({ component: Component, ...rest }) => { 
    <Route {...rest} render={props => (
      firebase.auth().onAuthStateChanged((user) => user 
      ?(
      store.dispatch(actions.login(user.uid)); 
      <Component {...props}/> 
     ) 
      :(
      store.dispatch(actions.logout()); 
      <Redirect to={{ 
       pathname: '/login', 
       state: { from: props.location } 
       }}/> 
      ) 
     ) 
     )}/> 
     } 

回答

1

普通括號((..))讓你返回一個值。這就是爲什麼你的語法錯了。你應該做下面的事情。

const PrivateRoute = ({ component: Component, ...rest }) => { 

    // return the Route component 
    return <Route {...rest} render={props => { 
    firebase.auth().onAuthStateChanged((user) => { 
     if(user) { 

     // run dispatch 
     store.dispatch(actions.login(user.uid)); 
     // return component 
     return <Component {...props} /> 

     } else { 

     // run dispatch 
     store.dispatch(actions.logout()); 
     // return component 
     return <Redirect to={{ pathname: '/login', state: { from: props.location } }} /> 

     } 
    }); 
    }} /> 
} 
相關問題