所以我有一个组件,"Spreadsheet",它从一个应用程序接口中抓取一些东西,但是一旦数据被获取,我想隐藏侧导航栏"SideNav“。我使用条件渲染的唯一方法似乎是这样做的,但是第一个条件被包装在一个折叠面板中,所以它可能会被夹在标签之间,所以条件渲染不能工作。条件渲染正在重置我的组件,因此获取的数据将丢失。想知道如何在不丢失状态的情况下有条件地呈现一些东西。附注:该元素是一个来自React-router的元素,所以这可能与它有关。代码如下:
render(){
let Spreadsheet =
<Route
path="/spreadsheet"
render={(props) =>
<Spreadsheet
selectedStation={this.state.selectedStation}
stationOptions={this.state.stationOptions}
start={this.state.start}
end={this.state.end}
data={this.data}
statusOptions={this.statusOptionsWithoutRaw}
dataSegments={this.state.dataSegments}
userSandboxes={this.state.userSandboxes}
refreshUserSandboxes={this.getUserSandboxes}
showSideNav={this.state.showSideNav}
toggleSideNav={(showSideNav) => this.setState({ showSideNav })}
/>}
/>
return(
<Container fluid={true}>
{this.state.showSideNav ?
<Row className="row--margin">
<Col md={2}>
<SideNav
stations={this.state.station}
setTab={this.setTab}
handleStation={this.handleStation}
stationOptions={this.state.stationOptions}
userSandboxes={this.state.userSandboxes}
/>
</Col>
<Col md={10}>
<div className="accordion" id="page">
<Card>
<Card.Header className="accordionTitle" as={Button} variant="link" data-toggle="collapse" data-target="#station">
Station: {this.state.selectedStation && this.state.selectedStation.value}
</Card.Header>
<Card.Body id="station" className="collapse show card-body" data-parent="#page">
{Spreadsheet}
</Card.Body>
</Card>
<Card>
<Card.Header className="accordionTitle" as={Button} variant="link" data-toggle="collapse" data-target="#analysis">
Analysis Package:
</Card.Header>
<Card.Body id="analysis" className="collapse card-body" data-parent="#page">
<AnalysisPackage />
</Card.Body>
</Card>
<Card>
<Card.Header className="accordionTitle" as={Button} variant="link" data-toggle="collapse" data-target="#moreInfo">
Click for More Station Information:
</Card.Header>
<Card.Body id="moreInfo" className="collapse card-body" data-parent="#page">
Hello! I'm another body
</Card.Body>
</Card>
</div>
</Col>
</Row>
:
<Row className="row--margin">
{Spreadsheet}
</Row>
}
</Container>
)
}发布于 2019-09-24 08:19:13
从外观上看,您基本上是在卸载并再次挂载Spreadsheet。这真的是你想要的吗?
如果你想要的只是隐藏侧边栏,为什么不只在它上面加上条件呢?喜欢
Row className="row--margin">
<Col md={2}>
{this.state.showSideNav && ( <-- LIKE THIS
<SideNav
stations={this.state.station}
setTab={this.setTab}
handleStation={this.handleStation}
stationOptions={this.state.stationOptions}
userSandboxes={this.state.userSandboxes}
/>
)}
</Col>
<Col md={10}>
<div className="accordion" id="page">
<Card>
<Card.Header className="accordionTitle" as={Button} variant="link" data-toggle="collapse" data-target="#station">
Station: {this.state.selectedStation && this.state.selectedStation.value}
</Card.Header>
<Card.Body id="station" className="collapse show card-body" data-parent="#page">
{Spreadsheet}
</Card.Body>
</Card>
<Card>
<Card.Header className="accordionTitle" as={Button} variant="link" data-toggle="collapse" data-target="#analysis">
Analysis Package:
</Card.Header>
<Card.Body id="analysis" className="collapse card-body" data-parent="#page">
<AnalysisPackage />
</Card.Body>
</Card>
<Card>
<Card.Header className="accordionTitle" as={Button} variant="link" data-toggle="collapse" data-target="#moreInfo">
Click for More Station Information:
</Card.Header>
<Card.Body id="moreInfo" className="collapse card-body" data-parent="#page">
Hello! I'm another body
</Card.Body>
</Card>
</div>
</Col>
</Row>https://stackoverflow.com/questions/58068682
复制相似问题