单击任何链接时如何使反应侧边栏关闭?

Vic*_*box 4 reactjs

我是 React 新手,试图让这个组件工作: https: //github.com/balloob/react-sidebar

它可以工作,只是当点击链接时侧边栏不会关闭。如何单击侧边栏链接关闭侧边栏?

来自index.js:

 render((
      <Router history={hashHistory}>
        <Route component={Nav}>
          <Route path="/pages/page1" component={Page1}/>
          <Route path="/pages/page2" component={Page2}/>
          <Route path="/pages/about" component={About} />
          <Route path="/users/firsttime" component={UsersFirsttime} />
          <Redirect from="/" to="/users/firsttime" />
        </Route>
      </Router>
    ), document.getElementById('app'));
Run Code Online (Sandbox Code Playgroud)

来自 nav.js:

  render () {
    const sidebarContent = <SidebarContent />;

    const contentHeader = <span>
        {this.state.docked || <a onClick={this.menuButtonClick} href="javascript:void(0);" style={styles.contentHeaderMenuLink}>=</a>}
        <span>This title</span>
      </span>;

    const sidebarProps = {
      sidebar: sidebarContent,
      docked: this.state.docked,
      sidebarClassName: 'custom-sidebar-class',
      open: this.state.open,
      touch: this.state.touch,
      shadow: this.state.shadow,
      pullRight: this.state.pullRight,
      touchHandleWidth: this.state.touchHandleWidth,
      dragToggleDistance: this.state.dragToggleDistance,
      transitions: this.state.transitions,
      onSetOpen: this.onSetOpen,
    };

    return (
      <Sidebar {...sidebarProps} >
        <MaterialTitlePanel title={contentHeader}>
          {this.props.children}
        </MaterialTitlePanel>
      </Sidebar>
    );
  }
Run Code Online (Sandbox Code Playgroud)

来自 sidebar_content.js:

const SidebarContent = (props) => {
  // const style = props.style ? {...styles.sidebar, ...props.style} : styles.sidebar;
  const style = props.style ? update(styles.sidebar, { $merge: props.style }) : styles.sidebar;

  // const links = [];

  const handle = () => {
    console.log('handle');
  };

  return (
    <MaterialTitlePanel title="Travel Guide Mobi" style={style}>
      <div style={styles.content}>
        <Link to="/pages/page1" >Page 1</Link>
        <div style={styles.divider} />
        <a key="key1" href="#" style={styles.sidebarLink}>Cities & Events</a>
        <a key="key2" href="#" style={styles.sidebarLink}>Set Travel Plans</a>
        <div style={styles.divider} />
        <a key="key3" href="#" style={styles.sidebarLink}>Edit Profile</a>
        <a key="key4" href="#" style={styles.sidebarLink}>Logout</a>
      </div>
    </MaterialTitlePanel>
  );
};
Run Code Online (Sandbox Code Playgroud)

我想也许可以写一些类似的东西<Link to="/pages/page1" onClick={self.props.closeMenu}>Page 1</Link>,但我不确定到底该怎么做?

Swa*_*nil 6

你的想法是正确的。您可以在导航组件中使用布尔状态变量来决定侧边栏的可见性。

导航js

class Nav extends Component {
    constructor() {
        super()
        this.state = {showSidebar: true} // Initially we want it to be visible
    }

    toggleSidebar = () => {
        this.setState({showSidebar: !this.state.showSidebar})
    }

    render() {
        return <Sidebar 
                 toggleSidebar={this.toggleSidebar}
                 showSidebar={this.state.showSidebar}
               />
    }
}
Run Code Online (Sandbox Code Playgroud)

侧边栏.js

class Sidebar extends Component {
    render() {
        const {toggleSidebar, showSidebar} = this.props
        return(
            <div>
                <Link to="/pages/page1" onClick={toggleSidebar}>Page 1</Link>
                {showSidebar?Code for open sidebar:Code for closed sidebar}
            </div>
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

您显然可以更改条件渲染部分。例如,如果您想通过 css 处理它,您可以根据sidebarVisible变量的值动态更改 className。

希望这可以帮助 :)