0
我有一個頂部導航(Home,About,Faq)的應用程序。只有在HOME路線上,我需要將菜單隱藏起來然後顯示在屏幕上。導航內置於App.jsx中,因此它出現在每一頁上。我如何啓用這個特定路線的揭示?在React中的一條路線上顯示導航組件
我揭示組件
import React, { Component } from 'react';
class NavScroll extends React.Component {
constructor(props){
super(props);
this.state={isHide:false};
this.hideBar = this.hideBar.bind(this)
}
hideBar(){
let {isHide} = this.state
window.scrollY > this.prev?
!isHide && this.setState({isHide:true})
:
isHide && this.setState({isHide:false})
this.prev = window.scrollY;
}
componentDidMount(){
window.addEventListener('scroll',this.hideBar);
}
componentWillUnmount(){
window.removeEventListener('scroll',this.hideBar);
}
render(){
let classHide=this.state.isHide?"hide":""
return <div className={"fixed "+classHide}>
</div>;
}
}
export default NavScroll;
App.jsx
import React, { Component } from 'react';
import NavLink from './global/NavLink.jsx';
import Footer from './global/Footer.jsx';
import NavScroll from './global/NavScroll.jsx';
var App = React.createClass({
render: function(){
return (
<div>
<div>
<NavLink to="/"><img className="logo" alt="HOME"></img></NavLink>
<NavLink to="/about" className="navMenuButton">ABOUT</NavLink>
<NavLink to="/faq" className="navMenuButton">FAQ</NavLink>
</div>
{ this.props.children }
<Footer />
</div>
);
}
});
export default App;
謝謝!我必須更具體,並使用this.props.location,所以它是:const location = this.props.location; –
'const {location} = this.props'與'const location = this.props.location'相同,它只是使用解構賦值。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Object_destructuring對於只有一個變量來說它並不是必須的,但是當你想要解構一個數字時它就派上用場了來自同一個對象的變量。 –
此外,如果這解決了您的問題,您可以將其標記爲正確的答案。 –