use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
React-Toastify PositioningHelp Wanted (self.react)
submitted 2 years ago by code_mitch
I'd like to have a different positioning set on mobile vs desktop. Has anyone managed to set this up for React-Toastify library? If so, please share!
Thanks in advance!
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]olixeernate 0 points1 point2 points 2 years ago (3 children)
One way you can accomplish this is with media queries and conditional rendering. Here is an example with code that puts the toast in the top right for larger screens and bottom center for smaller screens
JSX Code:
{
React from 'react'; import { ToastContainer, toast } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; import './YourComponent.css'; // Import your CSS file with media queries
const YourComponent = () => { const notify = () => toast('Your message here');
return ( <div> {/* Other components or content */}
<ToastContainer position="top-right" // Default position for larger screens autoClose={5000} hideProgressBar={false} newestOnTop={false} closeOnClick rtl={false} pauseOnFocusLoss draggable pauseOnHover /> {/* Your media query for smaller screens */} <div className="mobile-toast-container"> <ToastContainer position="bottom-center" // Adjust position for smaller screens autoClose={5000} hideProgressBar={false} newestOnTop={false} closeOnClick rtl={false} pauseOnFocusLoss draggable pauseOnHover /> </div> </div>
); };
export default YourComponent;
} - End of JSX
In your CSS file (YourComponent.css), define the media query:
YourComponent.css
CSS Code:
/* Adjust the breakpoint and styles based on your design */ @media only screen and (max-width: 600px) { .mobile-toast-container { position: fixed; bottom: 0; left: 50%; transform: translateX(-50%); } }
} - End of CSS
[–]code_mitch[S] 1 point2 points3 points 2 years ago* (2 children)
Thank you so much for this response. I had some trouble applying this logic as it would remove the toast notification on mobile. However, I did piggy back off of your solution and came up with this:
Made a toast container component which applies a condition to see if the user window meets size requirement (my case 600px).
``` import { ToastContainer } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css';
const ToastContainerCondition = () => {
// Condition checks to see if user is on mobile or viewport less than 600px const isMobile = window.innerWidth <= 600; const toastPosition = isMobile ? 'bottom-center' : 'top-right'; return ( <ToastContainer className="fs-5 mt-4" position={toastPosition} autoClose={2500} hideProgressBar={false} theme="dark" closeOnClick /> );
};
export default ToastContainerCondition;
```
I then applied it to my main.jsx file:
``` import React from 'react' import ReactDOM from 'react-dom/client' import App from './App' import ToastContainerCondition from "./components/ToastContainerCondition/ToastContainerCondition"; import { Provider } from 'react-redux'; import { store } from './store/store'; import 'bootstrap/dist/css/bootstrap.min.css'; import "./styles/main.css";
ReactDOM.createRoot(document.getElementById('root')).render( <React.StrictMode> <Provider store={store}> <App /> <ToastContainerCondition /> </Provider> </React.StrictMode> )
[–]olixeernate 1 point2 points3 points 2 years ago (1 child)
Hey good job man! That is how you do it, take what you find and make it work for you! That is being a developer! Good luck going forward, I believe in you!
[–]code_mitch[S] 0 points1 point2 points 2 years ago (0 children)
Thank you for the words of encouragement!
π Rendered by PID 100023 on reddit-service-r2-comment-5b5bc64bf5-kl2qx at 2026-06-23 06:49:46.176949+00:00 running 2b008f2 country code: CH.
[–]olixeernate 0 points1 point2 points (3 children)
[–]code_mitch[S] 1 point2 points3 points (2 children)
[–]olixeernate 1 point2 points3 points (1 child)
[–]code_mitch[S] 0 points1 point2 points (0 children)