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;
}
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
add a comment |
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
Tryhistory.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
add a comment |
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
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
reactjs firebase redirect browser-history
asked Jan 4 at 18:31
GunarathneMDDGunarathneMDD
57118
57118
Tryhistory.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
add a comment |
Tryhistory.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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 4 at 19:53
BreakBBBreakBB
745515
745515
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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