【问题标题】:Redirect happens before onClick() on REACT { Link }重定向发生在 REACT { Link } 上的 onClick() 之前
【发布时间】:2020-04-09 19:02:10
【问题描述】:

重定向发生在 REACT { Link } 上的 onClick() 之前

你好。我整天都在尝试播放声音 onClick Button Event。当我删除链接(URL)时它可以工作,但是通过链接,页面会转到新的 URL 而不会触发 onClick 事件。 我已经尝试过这样的:onClick={() => this.playAudioHandler}。 如果有人可以修复我的代码以便在转到新 URL 之前触发“playAudioHandler”,我真的很感激。

这是我的实际代码:

import React, { Component } from "react";
import { Link } from "react-router-dom";

import classes from "./Button.module.css";
import clickSound from "../assets/click.mp3";

class Button extends Component {
  playAudioHandler = (event) => {
    const audioEl = document.getElementsByClassName("audio-element")[0];
    audioEl.play();
    // event.preventDefault();
  };
  render() {
    return (
      <>
        <audio className="audio-element">
          <source src={clickSound}></source>
        </audio>
        <Link to={this.props.linkToGo}>
          <button
            type="submit"
            onClick={this.playAudioHandler}
            className={[classes.Button, classes[this.props.btnType]].join(" ")}
            // join() to transform the array in a string
          >
            {this.props.children}
          </button>
        </Link>
      </>
    );
  }
}

export default Button;

【问题讨论】:

  • 为什么有Link 组件包裹按钮?当您单击按钮时,它还会触发链接上的单击事件,从而使页面发生变化。如果只希望按钮播放声音,则不需要 Link 组件。
  • 那么你要先播放声音然后换页吗?
  • 我不希望按钮只是发出声音。这很容易。我想在单击按钮时更改页面。但我希望在更改 URL 之前执行“playAudioHandler”。这是来自父组件的“props.linkToGo”:

标签: reactjs hyperlink onclick


【解决方案1】:

链接会触发每次点击,因为这是它的工作方式。在您的情况下,按钮由 Link 包裹,因此 Link 被触发。
实现这一点的一个简单方法是避免使用 Link,因为看起来您正在使用 react-router-dom

<button
   type="submit"
   onClick={this.playAudioHandler}
   className={[classes.Button, 
   classes[this.props.btnType]].join(" ")}
>
   {this.props.children}
</button>

您的处理程序将在播放声音后重定向:

playAudioHandler = (event) => {
    event.preventDefault();
    const audioEl = document.getElementsByClassName("audio-element")[0];
    audioEl.play();

    const waitSecs = 3000 // 3 secs
    setTimeout(() => {
      history.push('/url');
      OR
      window.location.href = 'url'
    }, waitSecs);
  };

【讨论】:

  • 成功了!太感谢了。有趣的。我确实喜欢这样: playAudioHandler = (event) => { const audioEl = document.getElementsByClassName("audio-element")[0]; audioEl.play(); // event.preventDefault() const waitSecs = 300; // 3 秒 setTimeout(() => { window.location.href = "coronaquiz"; }, waitSecs); };很有意思。当我尝试 const waitSecs = 100 时,它没有用。这只是暂停它以播放声音的问题。非常感谢!
  • 很高兴它帮助了你!干杯!
【解决方案2】:

如何尝试设置一个定义重定向位置的状态并在单击按钮时设置该状态:

import React, { Component } from "react";
import { Redirect } from "react-router-dom";

import classes from "./Button.module.css";
import clickSound from "../assets/click.mp3";

class Button extends Component {
    constructor(super) {
        super(props);
        this.state = {
            redirect: null
        }
 }
 playAudioHandler = (event) => {
      const audioEl = 
   document.getElementsByClassName("audio-element")[0];
    audioEl.play();
    // event.preventDefault()
     this.setState({ redirect: this.props.linkToGo })
 };
 render() {
      if (this.state.redirect) {
       return <Redirect to={redirect} />
   }
   return (
  <>
    <audio className="audio-element">
      <source src={clickSound}></source>
    </audio>

      <button
        type="submit"
        onClick={ this.playAudioHandler}
        className={[classes.Button, 
        classes[this.props.btnType]].join(" ")}
        // join() to transform the array in a string
      >
           {this.props.children}
           </button>

           </>
        );
    }
}

export default Button;

让我知道它是否有效!

【讨论】:

  • 非常感谢您的尝试。同样的问题。它重定向而不播放声音。如果我收到一个“假” URL 作为道具,声音会播放,但当然,它不会重定向。播放声音的功能运行良好。问题是它在不播放声音的情况下重定向。我想知道在重定向之前是否有义务对执行 .play() 做出反应......
  • 你也许可以按照我的方式解决它,但使用衬衫超时来执行 setState 以进行重定向
  • 我将您的解决方案与 Adolfo Onrubia 解决方案混合使用。我将您的建议与“import { Redirect } from "react-router-dom";”一起使用加上常量 waitSecs = 300; setTimeout(() => { window.location.href = "coronaquiz"; }, waitSecs);
猜你喜欢
  • 2019-03-12
  • 2018-12-08
  • 2020-04-28
  • 1970-01-01
  • 2012-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-01
相关资源
最近更新 更多