代码之家  ›  专栏  ›  技术社区  ›  Rémy Kaloustian

ReactJS-向链接添加延迟

  •  0
  • Rémy Kaloustian  · 技术社区  · 8 年前

    我正在使用React和Redux。在某个时刻,我有一个链接,我想在更改路由之前为该链接添加一个延迟。

       render(){
            return(
                <div>
                <h3>New note for {this.props.match.params.user}</h3>
                <input  placeholder="Your note" onChange = {(e)=>this.handleChange(e)}/>
                <Link to={`/board/${this.props.match.params.user}`} >
                <br/>
                    <button onClick={(e) => {this.validateNote()}}>Add this now !</button>
                </Link>
                </div>
            );
        }
    

    我该怎么做?

    1 回复  |  直到 8 年前
        1
  •  1
  •   Matan Bobi    8 年前

    正如@shubham khatri在评论中所写的,我肯定会使用编程方式来导航,而不是使用链接组件。 看一看 here .

    为了回答特定的问题,由于链接中已经有一个按钮,我将使用它的回调来更改路由。

    <button onClick={(e) => {this.validateNote(); this.props.history.push(`/board/${this.props.match.params.user}`);}}>Add this now !</button>
    

    如果我们已经讨论过了,我不建议您使用匿名函数作为onClick的回调,因为这样可以在每次渲染时创建一个新函数。 试着读一下 here