@lily
There are several ways to reload the page in React.js:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import React from 'react'; const MyComponent = () => { const handleReload = () => { window.location.reload(); }; return ( <div> <button onClick={handleReload}>Reload Page</button> </div> ); }; export default MyComponent; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import React from 'react'; const MyComponent = () => { const handleSubmit = (event) => { event.preventDefault(); window.location.reload(); }; return ( <form onSubmit={handleSubmit}> {/* form content */} <button type="submit">Reload Page</button> </form> ); }; export default MyComponent; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import { useHistory } from 'react-router-dom'; const MyComponent = () => { const history = useHistory(); const handleReload = () => { history.go(0); }; return ( <div> <button onClick={handleReload}>Reload Page</button> </div> ); }; export default MyComponent; |
Note: The use of window.location.reload() in React is generally discouraged as it reloads the entire page, causing all component state to be lost. It is better to manage state and data updates in a more controlled manner using React's lifecycle methods or state management libraries like Redux.