React: File download
Got a requirement to save api response data (blob) to a file.
React: File download
Got a requirement to save api response data (blob) to a file.
React: Preload Images
I had a situation that lot of time taken to load images, which are loaded dynamically. One solution was to preload the images whenever we get the URL so that images will be available in cache and it will load quickly.
Below solution worked
References:
https://stackoverflow.com/questions/42615556/how-to-preload-images-in-react-js
Tailwind CSS: Dark and Light Theme
I am using Tailwindcss in my react app. One of the requirement was to provide light and dark theme for the web app. This can be achieved easily with Tailwind.
1. Specify colours and styles for light and dark theme. For light theme just use class-name as it is and for dark theme, use dark:class-name.
eg: bg-white dark:bg-gray-700
Switch between white and dark theme
To switch between the themes, there are many methods. I chose using class. Using class, need to mention darkMode: "class" in tailwind.config.js.
Then we can add a button, on click add/remove class-name "dark" to/from <html> to toggle between dark between light theme.
Note: To keep the theme selection, we can store the value to localstorage and retrieve it on load for better user experience.
Reference: https://tailwindcss.com/docs/dark-mode
React Router V6(react-router-dom) Dynamic Routing
I was trying to implement dynamic routing with router v6 similar to that with v4 .
eg in v4
<Route path={variable} component
This can be achieved in router v6 using useRoutes
Router.tsx
App.tsx
References:
https://reactrouter.com/en/main/hooks/use-routes
React : Disable Browser Forward and Backward Button or Prevent Navigation
To prevent navigation to specific route, we need to use history.block() - https://github.com/remix-run/history/blob/dev/docs/blocking-transitions.md
The browser backward button click action will be pop() and the browser forward button click action will be push(). We need to listen and handle these actions to block navigation.
"browserslist": { "production": [ ">0.2%", "not dead", "not op_mini all" ], "development": ["ie 11","last 1 chrome version", "last 1 firefox version", "last 1 safari version" ] }
4. Deleted node_modules
5 yarn and yarn start. Worked !!!
create-react-app myApp
4. Run the application
cd myApp
npm start
The application will be started at http://localhost:3000.