最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - history.listen not working correctly inside React Router - Stack Overflow

matteradmin6PV0评论

I want to be able to call a function every time React recognises the url has changed, but am having trouble with history.listen not firing (the console.log is never printed). I have seen posts saying that this is because I'm using BrowserRouter, but I am getting errors saying "cannot find x inside react-router" whenever I try importing anything from react-router. I currently have react-router and react-router-dom versions 4.1.2

App.js

import React, { Component } from 'react';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import ReduxThunk from 'redux-thunk';
import { BrowserRouter, Route, Redirect, Link } from 'react-router-dom';

import './App.css';
import reducers from './mon/reducers'
import Routes from './mon/Routes';
import RoutesWrapper from './ponents/RoutesWrapper';
import Login from './Login';

class App extends Component {

    render() {
        const store = createStore(reducers, {}, applyMiddleware(ReduxThunk));

        return (
            <Provider store={store}>
                <BrowserRouter>
                    <Login>
                        <div className="App">
                            <div className="widgetCont">
                                <Routes wrapper={RoutesWrapper}/>
                            </div>
                        </div>
                    </Login>
                </BrowserRouter>
            </Provider>
        );
    }
}

export default App;
export { Route, Redirect, Link };

Routes.js

import React, { Component } from 'react';
import createHistory from 'history/createBrowserHistory';

import { Route } from '../App.js';
import PredictionsCompletedContainer from './containers/PredictionsCompletedContainer';
import GameContainer from './containers/GameContainer';

class Routes extends Component {


    render() {
        const Wrapper = this.props.wrapper;

        const history = createHistory();
        history.listen((location, action) => {
            console.log("inside history listen");
        });

        return (
            <Wrapper>
                <Route exact path="/" ponent={GameContainer}/>
                <Route path="/predictions-pleted" ponent={PredictionsCompletedContainer}/>
            </Wrapper>
        );
    }
}

export default Routes;

I want to be able to call a function every time React recognises the url has changed, but am having trouble with history.listen not firing (the console.log is never printed). I have seen posts saying that this is because I'm using BrowserRouter, but I am getting errors saying "cannot find x inside react-router" whenever I try importing anything from react-router. I currently have react-router and react-router-dom versions 4.1.2

App.js

import React, { Component } from 'react';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import ReduxThunk from 'redux-thunk';
import { BrowserRouter, Route, Redirect, Link } from 'react-router-dom';

import './App.css';
import reducers from './mon/reducers'
import Routes from './mon/Routes';
import RoutesWrapper from './ponents/RoutesWrapper';
import Login from './Login';

class App extends Component {

    render() {
        const store = createStore(reducers, {}, applyMiddleware(ReduxThunk));

        return (
            <Provider store={store}>
                <BrowserRouter>
                    <Login>
                        <div className="App">
                            <div className="widgetCont">
                                <Routes wrapper={RoutesWrapper}/>
                            </div>
                        </div>
                    </Login>
                </BrowserRouter>
            </Provider>
        );
    }
}

export default App;
export { Route, Redirect, Link };

Routes.js

import React, { Component } from 'react';
import createHistory from 'history/createBrowserHistory';

import { Route } from '../App.js';
import PredictionsCompletedContainer from './containers/PredictionsCompletedContainer';
import GameContainer from './containers/GameContainer';

class Routes extends Component {


    render() {
        const Wrapper = this.props.wrapper;

        const history = createHistory();
        history.listen((location, action) => {
            console.log("inside history listen");
        });

        return (
            <Wrapper>
                <Route exact path="/" ponent={GameContainer}/>
                <Route path="/predictions-pleted" ponent={PredictionsCompletedContainer}/>
            </Wrapper>
        );
    }
}

export default Routes;
Share Improve this question asked Feb 20, 2018 at 15:10 jSutcliffe90jSutcliffe90 3232 gold badges6 silver badges19 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 1

With every re-rendering of ponent, render is getting called.

It causing, listener getting removed as you initialising history object.

Its better,put it inside ponent life cycle ponentDidMount which going to called only once in life.

ponentDidMount(){

    const history = createHistory();
        history.listen((location, action) => {
            console.log("inside history listen");
        });
}

EDIT :

Its look like,you need to integrate the react-router-redux.

Please refer answer here.

Post a comment

comment list (0)

  1. No comments so far