score:1
try link and prompt with react-router-dom
assuming you are using that:
import react from 'react'
import {
browserrouter as router,
route,
link,
prompt
} from 'react-router-dom'
const index = () => (
<router>
<div>
<ul>
<li><link to="/">form</link></li>
<li><link to="/one">one</link></li>
<li><link to="/two">two</link></li>
</ul>
<route path="/" exact component={form} />
<route path="/one" render={() => <h3>one</h3>} />
<route path="/two" render={() => <h3>two</h3>} />
</div>
</router>
)
class form extends react.component {
state = {
isblocking: false
}
render() {
const { isblocking } = this.state
return (
<form
style={{ margintop: '100px' }}
onsubmit={event => {
event.preventdefault()
event.target.reset()
this.setstate({
isblocking: false
})
}}
>
<prompt
when={isblocking}
message={location => (
`are you sure you want to go to ${location.pathname}`
)}
/>
<p>
blocking? {isblocking ? 'yes, click a link or the back button' : 'nope'}
</p>
<p>
<input
size="50"
placeholder="type something to block transitions"
onchange={event => {
this.setstate({
isblocking: event.target.value.length > 0
})
}}
/>
</p>
<p>
<button>submit to stop blocking</button>
</p>
<link to="/">click here to navigate</link>
</form>
)
}
}
export default index
i just tried running this code, and it worked in an existing project.
see: https://reacttraining.com/react-router/web/example/preventing-transitions
this will hook to your router and detect navigation change and throw the message via <prompt />
if it detects navigating away.
Source: stackoverflow.com
Related Query
- How to force user to save changes in ReactJS
- How to force re render when an array changes its value in ReactJS
- How to debounce user input in reactjs using rxjs
- How to check if a user is logged in and how to save their information? (React)
- How to upload image using ReactJS and save into local storage?
- How to get User information from JWT cookie in NextJS / ReactJS
- React: how to force state changes to take place after setState
- How to refresh Material-Table (Material-ui) component in ReactJS when data changes
- How can I show a different sidebar based on user information? After logged in. ReactJS
- How does SEO play into the ReactJS page changes
- How to force reactJS to redraw?
- ReactJS - How to allow user to type only integers
- How to save input entered by the user in a state which is an array in React
- How to make an array.length counter update on user scroll using ReactJS
- How to Redirect/go to the User Profile after login page in ReactJS
- How to force update child component from parent component in reactjs
- React hooks - how to force useEffect to run when state changes to the same value?
- Reactjs - How do I check to see if a user clicks a textbox within a table row?
- ReactJS - how can I handle notifications to user based on server response?
- How to get Current User using ReactJs
- In ReactJS How to save Child State in Parent state on destroying child?
- Using ReactJS how to subscribe multiple onSnapshot collection changes in Firestore
- How to save authenticated user in state using Redux and React?
- How i can save state ( data ) on Previous/Back" button click in ReactJS
- How to handle unAuthorized user and redirect it to 403 page in Reactjs
- ReactJS how to allow changes on an uncontrolled input?
- How can I restrict user from not to select more than three checkbox in reactjs
- ReactJS how can I save two numbers from one input field in a state
- How to use ReactJS to give immediate feedback to user before a long operation?
- How to use React (this.state) to achieve user input save automatically
More Query from same tag
- Target a css class in 'a component generated by library' in react
- Loop through array and render multiple instances of a child component with that data with React?
- how can I use nested css template literals in styled-components, emotion
- Compute the parents state based on the data of the child components: ReactJS
- How can I take a group of nested JSON objects and append their data to an editable text field?
- How do I fetch data (via GET) after some React state updates?
- dropdown is not open when using context
- Why does React call the render method of an unchanged component?
- WebpackError: TypeError: Object(...) is not a function gatsbyjs build error (botframework-webchat)
- Animations not working when entering (Tailwind, TransitionGroup, Typescript)
- Stores for reusable React+Flux form component (one component for multiple forms with different fields)
- useEffect is not getting call first time when parent state is changed by child state at
- Can I use react to create a subdomain application?
- Typescript based React: importScripts() gives: 'importScripts' is not defined no-undef
- Alternate between user and computer turn (react.js card game)
- Deployed my react app to github, but homepage is blank
- How can I render a component X amount of times based on a javascript object in React?
- Use custom hook in callback function
- Unable to get background service worker listener working - Error at parameter 'extraInfoSpec': Value must be one of extraHeaders, responseHeaders
- React Add Css to Component without addon library
- How to correctly use react-router
- How to add an svg image so that it highlights on hover
- How to populate array with values from API using "fetch" in javascript function
- What is the role of getIn with Formik?
- How can I export if / else if statements into the function inside Class component? react, node, express
- Should you test that all components render without crashing?
- Delay in filter input basic reactJS search function
- cannot delete array item, the wrong item always gets deleted
- Why anchor tag is taking extra white space?
- How to use tabs.executeScript with react