2016-12-06 4 views
0

상단에 탐색 기능이있는 앱 (집, 정보, 질문)이 있습니다. 홈 루트에서만 메뉴가 숨겨져 있고 스크롤이 나타납니다. 탐색은 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

당신은 현재 locationpathname 확인 할 수 있습니다
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; 

답변

0

밝혀 내. location은 일치 경로로 렌더링 된 구성 요소의 injected prop입니다.

class App extends React.Component { 

    render() { 
    const { location } = this.props 

    return (
     <div> 
     { location.pathname === '/' ? <HomeNavigation /> : <OtherNavigation /> } 
     </div> 
    ) 
    } 
} 
+0

감사합니다. 좀 더 구체적이고 this.props.location을 대신 사용해야했습니다. 그래서 const location = this.props.location; –

+0

'const {location} = this.props'는'const location = this.props.location'과 동일합니다. 단지 destructuring 할당을 사용합니다. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Object_destructuring 실제로 하나의 변수 만 필요한 것은 아니지만 숫자를 삭제하려는 경우 유용합니다 동일한 객체의 변수를 –

+0

또한이 방법으로 문제가 해결되면 정답으로 표시 할 수 있습니다. –

관련 문제