简体   繁体   English

如何在reactJs中检测包含特定类的子节点?

[英]How to detect a child node that contains a specific class in reactJs?

I have this renderization done by reactJS我通过 reactJS 完成了这个渲染

render() {使成为() {

    return (
      <div>
        <Router>
          <div className="wrapper2">
            <nav id="menu-principal" className="main menu-principal">
              <ul>
                <li><NavLink className="home-link" exact to="/">início</NavLink></li>
                <li><NavLink exact to="/nota_biografica">nota biográfica</NavLink></li>

              </ul>
            </nav>
            <div className="page-content">
              <Route exact path='/nota_biografica' render={() => <NotaBiografica />} />                  
              <Route exact path='/' render={() => <Home />} />
            </div>
          </div>
        </Router>
      </div>
    )
  }

When I click on the link that contains a "home-link" class, I would like to do some action.当我单击包含“home-link”类的链接时,我想做一些操作。 Like an action I tried to do in the example below which didn't work.就像我在下面的示例中尝试做的一个动作一样,但它不起作用。 How can I solve it?我该如何解决? Thank you.谢谢你。

var el = document.querySelector(".menu-principal");
      el.onclick = (event) => {                 
        for(var i = 0; i < el.childNodes.length; i++) {
          if(el.childNodes[i].className === "home-link"){
            alert('true')
          }
        }
      }

Because the element you targeted is <NavLink> tag which normally should navigate to another router page, I believe the event you have attached will not get running.因为您定位的元素是<NavLink>标签,通常应该导航到另一个路由器页面,所以我相信您附加的事件不会运行。 I suggest you to not using <NavLink> just use this.props.history.push('/path/to/route') instead on the onClick handler ( after the procedures you need to run before navigating to another routing address ).我建议您不要使用<NavLink>而是在onClick处理程序上使用this.props.history.push('/path/to/route') (在导航到另一个路由地址之前需要运行的程序之后)。

On the following example, Please pay attention about withRouter and i change <NavLink> to span then attach onHomeCLick handler to it:在以下示例中,请注意withRouter并将<NavLink>更改为 span 然后将onHomeCLick处理程序附加到它:

import React, {Component} from 'react';
import {withRouter, Route, BrowserRouter as Router} from "react-router-dom";
class YourComponent extends Component {

  onHomeCLick = () => {
    //DO something here prior to router navigation of ".home-link" element

    this.props.history.push( '/' );
  };

  render(){
    return (
      <div>
        <Router>
          <div className="wrapper2">
            <nav id="menu-principal" className="main menu-principal">
              <ul>
                <li><span className="home-link" onClick={this.onHomeCLick}>início</span></li>
                <li><NavLink exact to="/nota_biografica">nota biográfica</NavLink></li>

              </ul>
            </nav>
            <div className="page-content">
              <Route exact path='/nota_biografica' render={() => <NotaBiografica />} />
              <Route exact path='/' render={() => <Home />} />
            </div>
          </div>
        </Router>
      </div>
    )
  }

}

export default withRouter( YourComponent );

UPDATE===========更新============

Another trick if you like to use <NavLink> , the only solution is a "Call back" way.如果您喜欢使用<NavLink>另一个技巧,唯一的解决方案是“回调”方式。 It will trigger once you navigated to the route ( I still can't see if there is way to do it prior to navigation ).一旦您导航到路线,它就会触发(我仍然看不到在导航之前是否有办法做到这一点)。 Here we go:开始了:

import React, {Component} from 'react';
import {Route, BrowserRouter as Router} from "react-router-dom";

class YourComponent extends Component {

  isActive= (match, location) => {
    if (match) {
      console.log('Hi i am active, do something here');
    }
    return match;
  };

  render(){
    return (
      <div>
        <Router>
          <div className="wrapper2">
            <nav id="menu-principal" className="main menu-principal">
              <ul>
                <li><NavLink className="home-link" activeClassName="active" isActive={this.isActive}>início</NavLink></li>
                <li><NavLink exact to="/nota_biografica">nota biográfica</NavLink></li>

              </ul>
            </nav>
            <div className="page-content">
              <Route exact path='/nota_biografica' render={() => <NotaBiografica />} />
              <Route exact path='/' render={() => <Home />} />
            </div>
          </div>
        </Router>
      </div>
    )
  }

}

export default YourComponent;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM