当使用来自react-router-dom的Link时,为什么在'History'上无法执行'pushState'?

karolis2017

我看过类似的帖子,但找不到答案,就我而言,我正试图通过以下方式采取行动<App />

  addExpense = (expense) => {
    console.log('Hello From AddExpenseForm');
  }

/create路线在那里我渲染<AddExpenseForm />组件

<Link to={{
  pathname: '/create',
  state: { addExpense: this.addExpense }
}}> Create Expense</Link>

链接已呈现,但是当我单击它时,控制台出现错误:

Uncaught DOMException: Failed to execute 'pushState' on 'History': function (expense) {
      console.log('Hello From addExpense');
    } could not be cloned

为什么会这样,这里的解决方法是什么?

我更新的代码:

Index.js中的路由:

const Routes = () => {
  return (
      <BrowserRouter>
        <Switch>
          <Route path="/" exact component={App} />
          <Route path="/create" exact component={AddExpenseForm}/>
          <Route path="/expense/:expenseId" name="routename" component={ExpenseDetails} />
          <Route component={NotFound} />
        </Switch>
      </BrowserRouter>
  )
}

App.js:

  addExpense = (expense = {}) => {
    console.log('Hello From AddExpenseForm');
  }

  goToNextPage = (addExpense) => {
    this.props.history.push({
      pathname: '/create',
      state: { addExpense: this.addExpense }
    });
  }

  render() {
    return (
      <div>
        <Header
        />
        <button onClick={this.goToNextPage}>Create</button>
        ...
黑胡子

首先,向您的addExpense函数添加一些默认值

  addExpense = (expense = DEFAULT) => {
    console.log('Hello From addExpense');
  }

然后使用Link

<Link to={`/ideas/${this.addExpense}`}>Create Expense​</Link>         

尝试以下方法更好的方法):

  goToNextPage = (addExpense) => {
    this.props.history.push({
      pathname: '/create',
      state: { addExpense: addExpense }
    });
  }

在next(create)组件中,使用传递的值,如下所示:

this.props.location.state.addExpense();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用history.pushState将查询字符串变量追加到现有URL?

将后退按钮与history.pushstate和popstate一起使用时,如何触发更改?

iOS上的History API是否已损坏?(位置栏不会在pushState上更新)

使用history.pushState后检测浏览器后退按钮

警告:不推荐使用[history] pushState;使用推送代替

在jQuery Ajax上使用history.pushstate

window.location.href vs history.pushState-使用哪个?

history.pushState不会更新移动Chrome上“共享...”按钮的网址

使用history.pushState功能时如何启用后退和前进按钮?

在程序上使用llvm传递时,错误:无法执行命令:分段错误(核心已转储)

反应Auth状态和Routes。无法在React-Router的'History'上执行'replaceState'

使用AJAX时history.pushState出现问题

如何在angular上使用pushState?

为什么history.pushState()破坏了我的导航功能?

为什么在使用DispatchQueue时我的函数无法执行?

使用来自React-Router的onClick on Link

使用react-router-dom的history.push

在React Router中使用history.push()时需要刷新的问题

调用History.pushState时重复通话

使用History.pushstate时,如何防止每次单击链接时URL变长?

HTML history.pushState无法正常工作

使用history.pushState时防止404

如何使用window.history.pushState

使用第一个 url 多次使用 history.pushState

如何使用来自 react-router-dom 的 Mocha 测试“NavLink”

在 react-router 中使用 history.push() 传递道具时出现问题

如何使用来自 antd 表列的 React Router <Link to>

使用来自 react-router-dom 的路由时 ReactDOM 渲染出错

React-router-dom如何在不使用History的情况下使用goBack函数