我正在尝试制作自己的个人网站,并尝试使用 React 来实现。在这个过程中,我打算让每个部分成为不同的 React 组件。我的计划是让顶部的导航栏能够选择当前“活动”的组件,并实际呈现和显示。另外,当切换到新部分时,我希望旧组件有一个“离开”动画,而新组件有一个“进入”动画(这些都是通过 react-motion 完成的)。但是,目前进入和离开都是同时完成的,因为我同时更改了两个组件的活动状态。有什么方法可以延迟一个组件在另一个组件变为非活动状态后变为活动状态?
容纳每个部分的父组件如下所示:
class Website extends React.Component {
constructor(props){
super(props)
this.state = {
homeActive: true,
aboutActive: false
}
homeActivator(){
this.setState({
homeActive: true,
aboutActive: false
})
}
aboutActivator(){
this.setState({
homeActive: false,
aboutActive: true
})
}
render(){
return (
<div>
<NavBar handleHome={this.homeActivator.bind(this)} handleAbout=
{this.aboutActivator.bind(this)}/>
<Home active={this.state.homeActive} />
<About active={this.state.aboutActive} />
</div>
}
然后其中一个“部分”看起来像这样:
class Home extends React.Component{
render() {
let content = (
<div>
Home
</div>
)
if (!this.props.active){
return (
//Some jsx that results in the content leaving the page
)
}
return(
//Some jsx that results in the content entering the page
)
}
}