Javascript 在条件中基于条件呈现事物
因此,我试图实现Javascript 在条件中基于条件呈现事物,javascript,html,typescript,Javascript,Html,Typescript,因此,我试图实现@pnp/sp的占位符组件: public render(): React.ReactElement<Props> { let buttons; if (this.props.context.pageContext.web.permissions.hasPermission(SPPermission.addListItems)) { buttons = <div> <PrimaryButto
@pnp/sp
的占位符组件:
public render(): React.ReactElement<Props> {
let buttons;
if (this.props.context.pageContext.web.permissions.hasPermission(SPPermission.addListItems)) {
buttons = <div>
<PrimaryButton
onClick={this._fetchJobs.bind(this)} text="Refresh Jobs"
/>
<PrimaryButton
onClick={this._showCreateJobPanel.bind(this)}
text="Create New Job"
/>
</div>;
}
return (
<div className= { styles.container }>
{ this.state.locations ? (
<Placeholder
iconName='Edit'
iconText="Configure the Delivery Board"
description="Please configure your Delivery Board"
buttonLabel="Configure"
// onConfigure={this.props.fPropertyPaneOpen} />
/>
) : (
<WebPartTitle
displayMode={this.props.displayMode}
title={this.props.title}
updateProperty={this.props.updateProperty}
/>
{buttons} <-- line 216
)}
</div>
)
}
我很确定它必须用嵌套的括号{}来处理,但我不知道为什么或者如何解决它。在三元运算符的每种情况下只能放一个表达式。假设您使用的是react 16或更高版本,您可以通过将其包装在片段中来解决此问题:
{ this.state.locations ? (
<Placeholder
iconName='Edit'
iconText="Configure the Delivery Board"
description="Please configure your Delivery Board"
buttonLabel="Configure"
/>
) : (
<React.Fragment>
<WebPartTitle
displayMode={this.props.displayMode}
title={this.props.title}
updateProperty={this.props.updateProperty}
/>
{buttons}
</React.Fragment>
)}
{this.state.locations(
) : (
{按钮}
)}
在react 16之前,您可以使用div,也可以使用数组。这是一个数组:
{ this.state.locations ? (
<Placeholder
iconName='Edit'
iconText="Configure the Delivery Board"
description="Please configure your Delivery Board"
buttonLabel="Configure"
/>
) : (
[
<WebPartTitle
displayMode={this.props.displayMode}
title={this.props.title}
updateProperty={this.props.updateProperty}
/>,
buttons
]
)}
{this.state.locations(
) : (
[
,
按钮
]
)}
在三元运算符的每种情况下,只能放置一个表达式。假设您使用的是react 16或更高版本,您可以通过将其包装在片段中来解决此问题:
{ this.state.locations ? (
<Placeholder
iconName='Edit'
iconText="Configure the Delivery Board"
description="Please configure your Delivery Board"
buttonLabel="Configure"
/>
) : (
<React.Fragment>
<WebPartTitle
displayMode={this.props.displayMode}
title={this.props.title}
updateProperty={this.props.updateProperty}
/>
{buttons}
</React.Fragment>
)}
{this.state.locations(
) : (
{按钮}
)}
在react 16之前,您可以使用div,也可以使用数组。这是一个数组:
{ this.state.locations ? (
<Placeholder
iconName='Edit'
iconText="Configure the Delivery Board"
description="Please configure your Delivery Board"
buttonLabel="Configure"
/>
) : (
[
<WebPartTitle
displayMode={this.props.displayMode}
title={this.props.title}
updateProperty={this.props.updateProperty}
/>,
buttons
]
)}
{this.state.locations(
) : (
[
,
按钮
]
)}