score:1
I faced quite the similar issue on one of my apps and I think that the solution is to avoid using the react-router <Link /> component
if you can.
Instead of using the <Link />
component, I used the withRouter()
HOC provided by react-router
:
What you wrote :
Class MyComponent extends React.Component{
<Link onClick={(e) => { e.stopPropagation(); }} [...]>
...
</Link>
}
export default MyComponent;
Can be replaced by :
Class MyComponent extends React.Component{
navigate = (event, location) => {
event.preventDefault();
this.props.history.push(location);
}
<a onClick={(e, href) => { this.navigate(e, href) }} [...]>
...
</a>
}
export default withRouter(MyComponent);
This should prevent your link to catch others event at the document level and achieve the same result as <Link />
component.
score:2
Update: It seems that there's no simple way to do this without rendering the whole app inside an iframe
.
If you don't mind doing that though, it's easy enough (taken from How to set iframe content of a react component):
const Frame = ({ children }) => {
const [ref, setRef] = useState(null)
const mountNode = ref?.contentWindow.document.body
return (
<iframe title="iframe" ref={setRef}>
{mountNode && ReactDOM.createPortal(children, mountNode)}
</iframe>
)
}
Then, wrap your main app as a child of the Frame
component. Clicks inside an iframe
will not bubble up to the parent window, as detailed in Capture click on div surrounding an iframe.
Here's a CodeSandbox demo showing this approach.
Original (non-functioning) answer below
On your App
or Router
component:
const blockClicks = useCallback((e) => {
const { target } = e
const { nodeName, href } = target
if (
target.closest('#root-id') &&
nodeName === 'A' &&
href &&
(/^https?:\/\//.test(!href) ||
href.startsWith(window.location.origin)
)) {
// amend logic specific to your use case
// - we want to avoid blocking irrelevant clicks
e.stopPropagation()
}
})
useEffect(() => {
document.addEventListener('click', blockClicks, true)
return () =>
document.removeEventListener('click', blockClicks, true)
})
Source: stackoverflow.com
Related Query
- React Router Link reloading page : Conflict with external event
- React Router Link with params not reloading page with new data from componentDidMount and Redux axios data fetching
- React Router issue. Whenever login button is clicked. I am directed to a blank page with the homepage link appended to the login page link is made
- ReactJS: Go to anchor link in HTML with react router dom only after the full page load
- How to link an image to a page dynamically with React Router Dom?
- History.push a link to a new tab with react router
- Can I use react router Link with CardActionArea?
- How to navigate to another page with a smooth scroll on a specific id with react router and react scroll
- react router dom link with params dont allways work
- Testing React Router with Link
- Open external link with function in React
- React Router Link doesn't work with LeafletJS
- React router is not reloading page when url matches the same route
- React router - pass api data to the linked component to open with a new page
- React Redux with redux-observable use the router to navigate to a different page after async action complete
- External Link with React
- Django static file serving in conflict with React Router
- Testing components that contain react router Link with Enzyme
- React Router - Open Link on new Tab and Redirect to Home Page
- React Router Invariant failed when Route or Link is used in an external package
- React Router trigger re-load / re-mount component on redirecting to same page with different slug
- How to link to current page with different param in react router?
- How to make react router Link load page like anchor tag?
- Serving a static html page from a single page react app with react router
- How can I add multiple path for home page with react router dom 6?
- ReactJS: Go to anchor link in HTML with react router dom
- How to add Link from react router dom to the submit button with the default Submit validation
- React Router link to non React page in application
- Problem rendering Component in a different page with React Router
- Can't get active link to work with React Router v4
More Query from same tag
- React Starter Kit error - page not found
- Material-UI: classes isn't working with external SVG icons?
- How to paste number in separate text fields by material ui?
- How to insert contents in declared JSX Component in React?
- Password matching validation using the Joi schema in React not working
- Cannot destructure property 'desks' of '(0 , _react.useContext)(...)' as it is undefined
- run function once a value in an object has been updated
- How to stop react component from going past the top of the page when scrolling
- How to save response to localStore with Redux Thunk?
- Warning: Unreachable code , using Reactjs
- Create contexts in multiple files and share functions and states with each other
- React tutorial via PluralSight - props is not defined
- What is best approach to set data to component from API in React JS
- map div elements in two columns
- Assign numbers to alphabets in react js
- Use of prevState to change value in array of objects does not return expected value
- Omitting an optional parameter, which is before another optional parameter in React Router
- slice text in textbox after newline
- Typescript extract type of nested object in template type
- shopping cart array not rendering properly, react.js
- How to override global border color styles for disabled Material UI TextField?
- I want to understand why the data i am passing as props into the React functional component is showing as undefined?
- redux-form-material-ui using getRenderedComponent() throws always an error
- How to prevent user from going back in react-router-dom?
- React - Redux - this.props.fetch.map is not a function
- How send parameters with URL in react?
- React / Material-UI: Use variable reference in custom theme
- Test a simple button component that has a prop(className) passed in and check if the className is present in the document? Jest/React
- Cannot convert undefined or null to object reactjs
- Why can't I change my input value in React?