代码之家  ›  专栏  ›  技术社区  ›  George Mauer

使用MemoryRouter时如何知道当前url

  •  2
  • George Mauer  · 技术社区  · 6 年前

    作为实验 and based on some docs 我写了下面的测试

      test(`Can navigate MemoryRouter`, (done) => {
        const logAll = (p, ...args) => {
          console.log(`SOME OUTPUT`,p, ...args)
          return p.location.pathname
        }
    
        const div = document.createElement('div')
    
        const Test = () => (
          <MemoryRouter>
            <React.Fragment>
              <Link to="/foo/bar" id="click-me" />
              <Route render={logAll} />
            </React.Fragment>
          </MemoryRouter>
        )
        ReactDOM.render(React.createElement(Test), div)
        console.log('click')
        Simulate.click(div.querySelector('#click-me'))
      })
    

    我想是因为 logAll / (确实如此)然后在 /foo/bar

    我错过了什么?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Alejandro    6 年前

    你的测试还可以(你甚至不需要 done )然而,第二个论点 Simulate.click 错过了。如果您添加它,它将按您的预期工作:

    Simulate.click(
        div.querySelector('#click-me'),
        { button: 0 }
    );
    

    看看 the codebase Link 不进行没有 button=0 万一。

    Blame of this line 链接 tracks only left button click .