validateDOMNesting warning React

前端 未结 1 516
说谎
说谎 2021-01-18 06:36

I have a React component that is clickable as a whole, but also contains buttons inside.

So something like


  ...
  <         


        
相关标签:
1条回答
  • 2021-01-18 06:40

    Nesting anchor tags (which is what <Link /> transpiles to) is forbidden in HTML. Even if you get the desired result, it is only because your browser is clever and has its own work-arounds. However, that behavior cannot be guaranteed across all browsers and platforms.

    How seriously should I take this?

    As per the above, I'd say quite seriously.

    What would be the workaround?

    The following isn't a workaround, but what I consider to be the "proper" implementation.

    I'd programatically implement the navigation for the wrapping element and use the <Link /> for the buttons. So something like:

    navigate = () => {
      //push "path1" to history
    }
    
    render() {
      return(
        <div onClick={this.navigate}>
          <Link to="path2">Some button</Link>
          <Link to="path3">Some button</Link>
        </div>
      );
    }
    

    For more info about programatically navigating in React Router see one of the links below:

    • For React Router v1-v3: Link
    • For React Router v4: Link
    0 讨论(0)
提交回复
热议问题