2017-04-10 44 views
0

我有一個場景,我想在我的JSX中添加一個簡單的布爾值。這只是一個標誌,說一些事情是積極或不積極的。在JSX中添加布爾值

<NavItem href="#/gosomewhere" active > 

我不確定如何通過JSX中的'active'標誌。

我認爲這將很好地工作:

const active= location.pathname.match(/^\/gosomewhere/) ? "active" : ""; 

return(

       <NavItem eventKey={1} href="#/gosomewhere" {active} > 
        Go Somewhere 
       </NavItem> 
) 

但是我得到這個錯誤:

Module build failed: SyntaxError: Unexpected token, expected ... 

難道我莫名其妙地需要使用傳播運營商在這種情況下?

回答

3

你可以通過布爾值的方式與其他道具一樣,簡單地使用active={true|false}

const active = location.pathname.match(/^\/gosomewhere/); 
return(
    <NavItem eventKey={1} href="#/gosomewhere" active={active} > 
    Go Somewhere 
    </NavItem> 
) 
+1

is active = true相當於'active'? –

+1

@OliverWatkins是的,它只是jsx中的快捷方式 – madox2

1

答案爲「是活動=真只相當於‘主動’?」

是的,active={true}相當於active。只是標記active被推薦爲W3C recommandation

我發佈這個作爲答案,因爲我沒有足夠的評論聲望。

+1

雖然它在html標籤中推薦,但一般不建議在jsx中使用。我的意思是這取決於你需要做什麼 – madox2