为何react-single-page-navigation在含constructor与state的组件中失效?
开发问题记录
我目前正在学习React,接触到一款实用的npm包react-single-page-navigation。
刚接入这个包时所有功能运行正常,之后我计划把现有Web应用改造成响应式布局,因此针对小屏幕设备单独开发了移动端导航,实现了基础的下拉菜单交互。
最初这版移动端导航的核心功能是正常的:下拉菜单默认收起,点击带锚点引用的导航按钮时,可以通过平滑滚动跳转到对应页面区块。
为了实现下拉菜单的展开/收起交互,我给移动端导航组件新增了包含menuVisible状态的构造函数,同时补充了两个事件处理函数:
- 响应“展开下拉菜单”按钮的点击动作
- 响应下拉菜单外部区域的点击动作,实现通用的下拉菜单交互逻辑
这套交互本身运行正常,下拉菜单的展示效果也符合预期,但导航跳转功能却失效了:点击导航按钮时,页面只会在首页区块内上下偏移几像素,完全无法跳转到目标区块。
相关代码实现
功能正常的桌面端导航组件
import React, {Component} from 'react'; export default class DesktopNavigation extends Component { render() { return ( <nav className={this.props.sticky ? 'navbar navbar--sticky' : 'navbar'} id='mainNav'> <div className='navbar--logo-holder'> <img alt='logo' className='navbar--logo'/> <h1 className={this.props.sticky ? 'navbar--logo-text-sticky' : 'navbar--logo-text'}>Booyar</h1> </div> <ul className='navbar--link'> <div ref={this.props.refs.Home} onClick={() => this.props.goTo('Home')}> <div className={(this.props.activeElement === 'Home' ? 'active-page ' : '') + (this.props.sticky ? 'navbar--link-item-sticky' : 'navbar--link-item')}> Home </div> </div> <div ref={this.props.refs.About} onClick={() => this.props.goTo('About')}> <div className={(this.props.activeElement === 'About' ? 'active-page ' : '') + (this.props.sticky ? 'navbar--link-item-sticky' : 'navbar--link-item')}> About </div> </div> <div ref={this.props.refs.Services} onClick={() => this.props.goTo('Services')}> <div className={(this.props.activeElement === 'Services' ? 'active-page ' : '') + (this.props.sticky ? 'navbar--link-item-sticky' : 'navbar--link-item')}> Services </div> </div> <div ref={this.props.refs.Projects} onClick={() => this.props.goTo('Projects')}> <div className={(this.props.activeElement === 'Projects' ? 'active-page ' : '') + (this.props.sticky ? 'navbar--link-item-sticky' : 'navbar--link-item')}> Projects </div> </div> <div ref={this.props.refs.Contact} onClick={() => this.props.goTo('Contact')}> <div className={(this.props.activeElement === 'Contact' ? 'active-page ' : '') + (this.props.sticky ? 'navbar--link-item-sticky' : 'navbar--link-item')}> Contact </div> </div> </ul> </nav> ); } }
跳转功能异常的移动端导航体系组件
// 移动端导航主组件 import React, {Component} from 'react'; import DropdownMenu from "./dropdownMenu"; export default class MobileNavigation extends Component { constructor(props) { super(props); this.state = { sticky: this.props.sticky } } callbackIsSticky = (isSticky) => { this.setState({sticky: isSticky}) } render() { return ( <nav className={this.state.sticky || this.props.sticky ? "mobile-navbar mobile-navbar-sticky" : "mobile-navbar"} id="mainNav"> <div className="mobile-navbar--logo-holder"> <img alt="logo" className="mobile-navbar--logo"/> </div> <DropdownMenu refs={this.props.refs} activeElement={this.props.activeElement} goTo={this.props.goTo} sticky={this.props.sticky} callback={this.callbackIsSticky} /> </nav> ); } }
// 移动端下拉菜单组件 import React, {Component} from 'react'; export default class DropdownMenu extends Component { constructor(props) { super(props); this.state = { menuVisible: false } this.handleClick = this.handleClick.bind(this); this.handleOutsideClick = this.handleOutsideClick.bind(this); } handleClick() { if (!this.state.menuVisible) { document.addEventListener('click', this.handleOutsideClick, false); this.props.callback(true); } else { document.removeEventListener('click', this.handleOutsideClick, false); this.props.callback(false); } this.setState(stateBefore => ({ menuVisible: !stateBefore.menuVisible, })); } handleOutsideClick(event) { if (this.node.contains(event.target)) { return; } this.handleClick(); } render() { const items = <div className={'dropdown-items'}> <button ref={this.props.refs.Home} onClick={() => this.props.goTo('Home')} className={(this.props.activeElement === 'Home' ? "active-mobile-page " : "")}> Home </button> <button ref={this.props.refs.About} onClick={() => this.props.goTo('About')} className={(this.props.activeElement === 'About' ? "active-mobile-page " : "")}> About </button> <button ref={this.props.refs.Services} onClick={() => this.props.goTo('Services')} className={(this.props.activeElement === 'Services' ? "active-mobile-page " : "")}> Services </button> <button ref={this.props.refs.Projects} onClick={() => this.props.goTo('Projects')} className={(this.props.activeElement === 'Projects' ? "active-mobile-page " : "")}> Projects </button> <button ref={this.props.refs.Contact} onClick={() => this.props.goTo('Contact')} className={(this.props.activeElement === 'Contact' ? "active-mobile-page " : "")}> Contact </button> </div>; return ( <div className={'dropdown-menu' + (this.state.menuVisible ? ' dropdown-menu-open' : '')} ref={node => this.node = node}> <button className={'hamburger hamburger--elastic' + (this.state.menuVisible ? ' is-active' : '')} onClick={this.handleClick} type={'button'}> <span className="hamburger-box"> <span className={'hamburger-inner' + (this.state.menuVisible || this.props.sticky ? ' hamburger-inner-sticky' : '')}> </span> </span> </button> {this.state.menuVisible ? items : undefined} </div> ); } }
全局配置说明
我已经按照react-single-page-navigation的官方文档要求,把所有页面内容包裹在<ScrollNavigation elements={{Home: {}, About: {}, Services: {}, Projects: {}, Contact: {}}}>...</ScrollNavigation>组件中。
同时通过CSS样式配合以下导航切换组件,实现移动端/桌面端导航的显示控制:
import React, {Component} from 'react'; import MobileNavigation from "./mobileNavigation"; import DesktopNavigation from "./desktopNavigation"; export default class Navigation extends Component { render() { return ( <div> <MobileNavigation refs={this.props.refs} activeElement={this.props.activeElement} goTo={this.props.goTo} sticky={this.props.isSticky}/> <DesktopNavigation refs={this.props.refs} activeElement={this.props.activeElement} goTo={this.props.goTo} sticky={this.props.isSticky}/> </div> ); } }
初步排查结论
现有代码逻辑下,桌面端导航完全符合预期,移动端导航仅能在首页区块内小幅偏移。我推测问题出在尚不熟悉的React基础规则上:如果注释掉上述除render方法外的所有逻辑(以及render方法中关联这些被注释逻辑的代码),移动端导航就能恢复正常跳转。
内容的提问来源于stack exchange,提问作者maximotus
相关产品推荐
相关产品推荐

