score:1
Accepted answer
with react-router-dom
you can create a history object (using the history
package) and pass this to the router
component. therefore if you create the history object like so and attach this to the window then pass the created history to the app
component:
import { createbrowserhistory as createhistory } from "history";
const customhistory = createhistory();
window.customhistory = customhistory;
reactdom.render(
<app history={customhistory} />,
document.getelementbyid("root")
);
in the app.js component:
import { router, route, switch } from "react-router-dom";
export default function app({ history }) {
return (
<router history={history}>
<switch>
<route exact path="/" component={page1 } />
<route path="/page2" component={page2 } />
<route path="/page3" component={page3 } />
</switch>
</router>
);
}
now you can use that history
object to change the page outside of the application:
$('#link_page2').click(function() {
console.log('navigating the app to: page1...');
window.customhistory.push("/page2");
});
Source: stackoverflow.com
Related Query
- How to make the ReactJS App to navigate to some pages from outside the app
- How can I call methods on Reactjs components in the browser console or from an outside script?
- how to use a for loop in ReactJS to update the state on meeting some condition? loops are not requiring to traverse from every element
- How to make a rest post call from ReactJS code?
- How to access the "key" property from a reactjs component
- How to get the data from React Context Consumer outside the render
- How to properly make REST calls from ReactJS + Redux application?
- How to make Puppeteer work with a ReactJS application on the client-side
- How to fetch data only once in a Next.js app and make it accesible to all the app, both in server and client
- How can I get the URL address from the browser in ReactJS (servers URL)
- Reactjs how to change the state of a component from a different component
- How to make ASP.NET/React app serve SPA from subpath?
- Reactjs - Get a component from anywhere in the app
- How to use the search filter from outside the table?
- How to take variable from outside reactjs
- How to make a npm package from Create React App /build folder?
- How to distinguish what caused rendering of React component: something from the inside or from the outside of the component?
- How are the process.env.NODE_ENV checks removed from ReactJS production builds?
- How do I change a Formik TextField value from the outside of the From?
- How to make the background color of an app cover the entire screen (using style) in React Native?
- How to display IP camera feed from an RTSP url onto reactjs app page?
- How to prevent outside CSS from adding and overriding ReactJS component styles
- How to prevent react-route from re-render the whole app when changing url
- How can I drag and drop objects into a Mapbox map from outside the map?
- How to make the Service Worker cache data from API and update the cache when needed
- In a ReactJS app, how to substitute env vars from the global environment into my index.html file?
- How to detect matched route from a component outside of the <Route/> component that was matched using react-router?
- How to make my react-multi-carousel use a light-box feature for my ReactJS app
- How to inject data from the server in index.html in an ASP.NET Core 3 React app
- How to navigate from login page to dashboard using reactjs
More Query from same tag
- Get component's height every time it renders
- Different wrap text between horizontal and vertical
- Height in theme ui embed
- Link of react-router-dom is not working in firefox
- 'Symbol' is undefined in IE after using babel
- React Native Reload and Dev Tools Do Not Work
- How to pass props via Link in React (this.props.location is undefined)
- React Konva Rotation/Revolution Animation
- How can I optimize my code to stop sending GET requests constantly?
- @material-ui Autocomplete: set input value programmatically
- Running npm run eject to use css modules
- How to handle multiple onChange function from multiple input fields in Reactjs
- Looping through Nested array
- How can I re write this code in the correct "react" way
- Show total number of selected filters array of items in React
- Faced error during testing with Jest and Enzyme in React
- $ yarn install leads to Cannot find module yarn-berry.js
- Style JSX in NextJS project does not work with external (antd) components
- Docker React App: 404s will fallback to /
- Maximum update depth exceeded. This can happen when a component repeatedly calls setState
- Meteor / ReactJS - UI blinking issue: rendering twice before and after checking a database
- Material UI Autocomplete component issue working with check
- onChange return undefined in ReactJS
- React calculations in render vs in lifecycle methods
- Using useEffect hook with "async"
- Npm @babel/types error in react typescript
- when i update the state the result is not updated until i refresh the page by using useEffect hook
- How to convert string to jsx?
- jsx --watch converts jsx syntax to lowercase "react" instead of uppercase "React"
- When specified, "proxy" in package.json must be a string