【问题标题】:How to Ignore Links in React Router?如何忽略 React Router 中的链接?
【发布时间】:2016-08-18 18:33:35
【问题描述】:

我正在尝试在我的应用程序中实现反应路由器,但我遇到了一个问题,我有一些显示“模式”对话框的“链接”,但反应路由器将它们视为路由并将我送回我的“家”页面

  <a href="#" onClick={() => this.props.Show(true) }><i className="fa fa-plus" aria-hidden="true">

路由器

ReactDOM.render(
    <Provider store={store}>
      <Router history={hashHistory}>
      <Route path="/" component={Layout}>
        <IndexRoute component={Home}></IndexRoute>
        <Route path="app" name="app" component={App}></Route>
      </Route>
    </Router>
    </Provider>,
    document.getElementById('root')
);

该链接位于“App”组件中的一个组件中。

编辑

我现在有这个

 return (
            <a href="#" id="add-new-storage-item-btn" className={addNewItemClasses} onClick={(event) => this.setAddStorageItemModal(event) }><i className="fa fa-plus" aria-hidden="true"> Add New Item</i></a>
        );

  setAddStorageItemModal(event)
   {
       this.props.setAddStorageItemModal(true);
       event.stopPropagation();

   }

我尝试将 event.stopPropagation 作为第一行但不起作用。

【问题讨论】:

    标签: javascript reactjs react-router


    【解决方案1】:

    如果要阻止锚标签导航,建议添加

    event.stopPropagation()

    到您的onClick 事件处理程序。

    【讨论】:

    • 对了,我尝试返回false;但这似乎不起作用,所以我什至没有尝试过,但它确实有效。
    • 您还可以将onClick() 分配给不是锚标签的元素
    • 奇怪的认为它正在工作,但现在它似乎没有工作。
    • 似乎我必须使用 preventDefault 才能使其工作。
    猜你喜欢
    • 2019-12-25
    • 2017-05-26
    • 2021-07-28
    • 2020-03-21
    • 1970-01-01
    • 2022-07-21
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    相关资源
    最近更新 更多