How to set React V4 BrowserHistory to work?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















My React version is '^16.6.1' and Router version is '^4.3.1'.



Before V4 I could route through pages programmatically using browserhistory.



But now I can't do it. I did it using import createHistory from 'history/createBrowserHistory'. It changes the url as I want. But it didn't change the page. It stays in the previous page.



Please give me a solution.



This is my code.



//index.js



import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import { BrowserRouter as Router } from "react-router-dom";

ReactDOM.render(
<Router>
<App />
</Router>,
document.getElementById("root")
);
serviceWorker.unregister();


//App.js



import React, { Component } from 'react';
import './App.css';
import createHistory from 'history/createBrowserHistory'
import { Route, Redirect } from 'react-router-dom';
import firebase from './firebase';
import Routes from "./Routes";
import NavigationBar from './components/NavigationBar/NavigationBar';

class App extends Component {

constructor() {
super();
this.state = { isLogged: false };
let ctx = this;
const history = createHistory();
firebase.auth().onAuthStateChanged(function (user) {
if (user) {
// User is signed in.
console.log("logged in");
history.replace('/home');
ctx.setState({ isLogged : true});
} else {
// No user is signed in.
console.log("not logged in");
ctx.setState({ isLogged : false});
}
});
}

render() {
return (
<div className="App">
<NavigationBar />
<Routes />
</div>
)
}
}

export default App;


//Routes.js



import React from 'react';
import { Route, Switch } from "react-router-dom";
import Login from './components/Admin/Login';
import Home from './components/Home/Home';
import Orchids from './components/Orchids/Orchids';
import Anthurium from './components/Anthurium/Anthurium';
import ContactUs from './components/ContactUs/ContactUs';
import AddOrchid from './components/Admin/AddOrchid';

export default (props) =>
<Switch>
{/* <Route path="/" exact component={LandingPage} /> */}
<Route path="/" exact component={Home} />
<Route path="/home" exact component={Home} />
<Route path="/orchids" component={Orchids} />
<Route path="/anthurium" component={Anthurium} />
<Route path="/contact-us" component={ContactUs} />
<Route path="/login" component={Login} />
<Route path="/add-orchid" component={AddOrchid} />
</Switch>;









share|improve this question























  • Try history.push('/home');

    – ma_dev_15
    Jan 4 at 18:37











  • I add both history.push() & history.replace(). But the result is same. The only URL is changed without page changed.

    – GunarathneMDD
    Jan 4 at 18:49


















0















My React version is '^16.6.1' and Router version is '^4.3.1'.



Before V4 I could route through pages programmatically using browserhistory.



But now I can't do it. I did it using import createHistory from 'history/createBrowserHistory'. It changes the url as I want. But it didn't change the page. It stays in the previous page.



Please give me a solution.



This is my code.



//index.js



import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import { BrowserRouter as Router } from "react-router-dom";

ReactDOM.render(
<Router>
<App />
</Router>,
document.getElementById("root")
);
serviceWorker.unregister();


//App.js



import React, { Component } from 'react';
import './App.css';
import createHistory from 'history/createBrowserHistory'
import { Route, Redirect } from 'react-router-dom';
import firebase from './firebase';
import Routes from "./Routes";
import NavigationBar from './components/NavigationBar/NavigationBar';

class App extends Component {

constructor() {
super();
this.state = { isLogged: false };
let ctx = this;
const history = createHistory();
firebase.auth().onAuthStateChanged(function (user) {
if (user) {
// User is signed in.
console.log("logged in");
history.replace('/home');
ctx.setState({ isLogged : true});
} else {
// No user is signed in.
console.log("not logged in");
ctx.setState({ isLogged : false});
}
});
}

render() {
return (
<div className="App">
<NavigationBar />
<Routes />
</div>
)
}
}

export default App;


//Routes.js



import React from 'react';
import { Route, Switch } from "react-router-dom";
import Login from './components/Admin/Login';
import Home from './components/Home/Home';
import Orchids from './components/Orchids/Orchids';
import Anthurium from './components/Anthurium/Anthurium';
import ContactUs from './components/ContactUs/ContactUs';
import AddOrchid from './components/Admin/AddOrchid';

export default (props) =>
<Switch>
{/* <Route path="/" exact component={LandingPage} /> */}
<Route path="/" exact component={Home} />
<Route path="/home" exact component={Home} />
<Route path="/orchids" component={Orchids} />
<Route path="/anthurium" component={Anthurium} />
<Route path="/contact-us" component={ContactUs} />
<Route path="/login" component={Login} />
<Route path="/add-orchid" component={AddOrchid} />
</Switch>;









share|improve this question























  • Try history.push('/home');

    – ma_dev_15
    Jan 4 at 18:37











  • I add both history.push() & history.replace(). But the result is same. The only URL is changed without page changed.

    – GunarathneMDD
    Jan 4 at 18:49














0












0








0








My React version is '^16.6.1' and Router version is '^4.3.1'.



Before V4 I could route through pages programmatically using browserhistory.



But now I can't do it. I did it using import createHistory from 'history/createBrowserHistory'. It changes the url as I want. But it didn't change the page. It stays in the previous page.



Please give me a solution.



This is my code.



//index.js



import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import { BrowserRouter as Router } from "react-router-dom";

ReactDOM.render(
<Router>
<App />
</Router>,
document.getElementById("root")
);
serviceWorker.unregister();


//App.js



import React, { Component } from 'react';
import './App.css';
import createHistory from 'history/createBrowserHistory'
import { Route, Redirect } from 'react-router-dom';
import firebase from './firebase';
import Routes from "./Routes";
import NavigationBar from './components/NavigationBar/NavigationBar';

class App extends Component {

constructor() {
super();
this.state = { isLogged: false };
let ctx = this;
const history = createHistory();
firebase.auth().onAuthStateChanged(function (user) {
if (user) {
// User is signed in.
console.log("logged in");
history.replace('/home');
ctx.setState({ isLogged : true});
} else {
// No user is signed in.
console.log("not logged in");
ctx.setState({ isLogged : false});
}
});
}

render() {
return (
<div className="App">
<NavigationBar />
<Routes />
</div>
)
}
}

export default App;


//Routes.js



import React from 'react';
import { Route, Switch } from "react-router-dom";
import Login from './components/Admin/Login';
import Home from './components/Home/Home';
import Orchids from './components/Orchids/Orchids';
import Anthurium from './components/Anthurium/Anthurium';
import ContactUs from './components/ContactUs/ContactUs';
import AddOrchid from './components/Admin/AddOrchid';

export default (props) =>
<Switch>
{/* <Route path="/" exact component={LandingPage} /> */}
<Route path="/" exact component={Home} />
<Route path="/home" exact component={Home} />
<Route path="/orchids" component={Orchids} />
<Route path="/anthurium" component={Anthurium} />
<Route path="/contact-us" component={ContactUs} />
<Route path="/login" component={Login} />
<Route path="/add-orchid" component={AddOrchid} />
</Switch>;









share|improve this question














My React version is '^16.6.1' and Router version is '^4.3.1'.



Before V4 I could route through pages programmatically using browserhistory.



But now I can't do it. I did it using import createHistory from 'history/createBrowserHistory'. It changes the url as I want. But it didn't change the page. It stays in the previous page.



Please give me a solution.



This is my code.



//index.js



import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import { BrowserRouter as Router } from "react-router-dom";

ReactDOM.render(
<Router>
<App />
</Router>,
document.getElementById("root")
);
serviceWorker.unregister();


//App.js



import React, { Component } from 'react';
import './App.css';
import createHistory from 'history/createBrowserHistory'
import { Route, Redirect } from 'react-router-dom';
import firebase from './firebase';
import Routes from "./Routes";
import NavigationBar from './components/NavigationBar/NavigationBar';

class App extends Component {

constructor() {
super();
this.state = { isLogged: false };
let ctx = this;
const history = createHistory();
firebase.auth().onAuthStateChanged(function (user) {
if (user) {
// User is signed in.
console.log("logged in");
history.replace('/home');
ctx.setState({ isLogged : true});
} else {
// No user is signed in.
console.log("not logged in");
ctx.setState({ isLogged : false});
}
});
}

render() {
return (
<div className="App">
<NavigationBar />
<Routes />
</div>
)
}
}

export default App;


//Routes.js



import React from 'react';
import { Route, Switch } from "react-router-dom";
import Login from './components/Admin/Login';
import Home from './components/Home/Home';
import Orchids from './components/Orchids/Orchids';
import Anthurium from './components/Anthurium/Anthurium';
import ContactUs from './components/ContactUs/ContactUs';
import AddOrchid from './components/Admin/AddOrchid';

export default (props) =>
<Switch>
{/* <Route path="/" exact component={LandingPage} /> */}
<Route path="/" exact component={Home} />
<Route path="/home" exact component={Home} />
<Route path="/orchids" component={Orchids} />
<Route path="/anthurium" component={Anthurium} />
<Route path="/contact-us" component={ContactUs} />
<Route path="/login" component={Login} />
<Route path="/add-orchid" component={AddOrchid} />
</Switch>;






reactjs firebase redirect browser-history






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 4 at 18:31









GunarathneMDDGunarathneMDD

57118




57118













  • Try history.push('/home');

    – ma_dev_15
    Jan 4 at 18:37











  • I add both history.push() & history.replace(). But the result is same. The only URL is changed without page changed.

    – GunarathneMDD
    Jan 4 at 18:49



















  • Try history.push('/home');

    – ma_dev_15
    Jan 4 at 18:37











  • I add both history.push() & history.replace(). But the result is same. The only URL is changed without page changed.

    – GunarathneMDD
    Jan 4 at 18:49

















Try history.push('/home');

– ma_dev_15
Jan 4 at 18:37





Try history.push('/home');

– ma_dev_15
Jan 4 at 18:37













I add both history.push() & history.replace(). But the result is same. The only URL is changed without page changed.

– GunarathneMDD
Jan 4 at 18:49





I add both history.push() & history.replace(). But the result is same. The only URL is changed without page changed.

– GunarathneMDD
Jan 4 at 18:49












1 Answer
1






active

oldest

votes


















0














Since you're already using BrowserRouter from the react-router-dom module you can simply use this.props.history.push("/yourPath") in children of the BrowserRouter component.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54044308%2fhow-to-set-react-v4-browserhistory-to-work%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Since you're already using BrowserRouter from the react-router-dom module you can simply use this.props.history.push("/yourPath") in children of the BrowserRouter component.






    share|improve this answer




























      0














      Since you're already using BrowserRouter from the react-router-dom module you can simply use this.props.history.push("/yourPath") in children of the BrowserRouter component.






      share|improve this answer


























        0












        0








        0







        Since you're already using BrowserRouter from the react-router-dom module you can simply use this.props.history.push("/yourPath") in children of the BrowserRouter component.






        share|improve this answer













        Since you're already using BrowserRouter from the react-router-dom module you can simply use this.props.history.push("/yourPath") in children of the BrowserRouter component.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 4 at 19:53









        BreakBBBreakBB

        745515




        745515
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54044308%2fhow-to-set-react-v4-browserhistory-to-work%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas