如何调试React Router?

时间:2015-12-04 17:17:39

标签: hyperlink reactjs react-router

我试图在我的反应应用程序中使用React Router,它受限于wordpress插件并使用flux来获取api数据。

我的入口点如下:

import React from 'react';
import Workshops  from './components/workshops';
import Workshop  from './components/workshop';
import NotFound  from './components/notfound';
import Router, { Route, DefaultRoute, NotFoundRoute, Redirect, Link } from 'react-router';
import json  from './config.json';
localStorage.clear();
localStorage.setItem('workshops', JSON.stringify(json));

const AppRoutes = (
   <Route path="/" handler={Workshops}>
    <DefaultRoute handler={Workshop} />
    <Route name="workshop" path=":slug" handler={Workshop}/>
    <NotFoundRoute handler={NotFound} />
  </Route>
);

Router.run(AppRoutes, Router.HashLocation, (Root) => {
  React.render(<Root />, document.getElementById('workshop-booker'));
});

比我的Workshops组件中我做了一些给定路由的链接,我有哈希更改,但路由组件没有被触发。

<h3> <Link to="workshop" params={{slug: workshop.slug }}> {workshop.title.rendered }</Link></h3>

4 个答案:

答案 0 :(得分:11)

您可以使用DebugRouter包装路由器,该路由器将打印出执行的导航操作:

import React, { Component } from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import Login from 'components/Login'
import DefaultComponent from 'components/DefaultComponent'

class DebugRouter extends Router {
  constructor(props){
    super(props);
    console.log('initial history is: ', JSON.stringify(this.history, null,2))
    this.history.listen((location, action)=>{
      console.log(
        `The current URL is ${location.pathname}${location.search}${location.hash}`
      )
      console.log(`The last navigation action was ${action}`, JSON.stringify(this.history, null,2));
    });
  }
}

class App extends Component {
  render() {
    return (
      <DebugRouter>
        <Switch>
          <Route exact path="/login" name="Login Page" component={Login} />
          <Route path="/" name="Home" component={DefaultComponent} />
        </Switch>
      </DebugRouter>
    );
  }
}

link to the gist

答案 1 :(得分:1)

您可以尝试这样的事情

import React, {Component} from 'react';
import PropTypes from 'prop-types'
import {withRouter} from "react-router-dom";

class RouterDebugger extends Component {
    componentWillUpdate(nextProps, nextState){
        console.log('componentWillUpdate',nextProps, nextState)
    }
    componentDidUpdate(prevProps) {
        console.log('componentDidUpdate',prevProps)

    }

    render() {
        return null
    }
}

export default withRouter(RouterDebugger)

将此组件插入要调试的任何位置。 您可以传递带有一些标识符的道具 我希望这能帮到你

答案 2 :(得分:1)

我为功能组件制作了DebugRouter

const DebugRouter = ({ children }: { children: any }) => {
  const { location } = useHistory()
  if (process.env.NODE_ENV === 'development') {
    console.log(
      `Route: ${location.pathname}${location.search}, State: ${JSON.stringify(location.state)}`,
    )
  }

  return children
}

const Router = () => {
  return (
    <BrowserRouter>
      <Layout>
        <Route
          render={() => {
            return (
              <DebugRouter>
                <Switch>
                  <Redirect exact from="/" to={...} />
                  // <Route/> should be here

                  <Redirect from="*" to={...} />
                </Switch>
              </DebugRouter>
            )
          }}
        />
      </Layout>
    </BrowserRouter>
  )
}

答案 3 :(得分:-2)

您可以使用以下代码调试React Router:

console.log(this.props.location)
console.log(this.props.match)