React vite show splash screen on app first loading
- User
React vite show splash screen on app first loading
- Assistant
React vite show splash screen on app first loading
To implement a splash screen in a React app using Vite, you'll need to follow a few steps. A splash screen is typically a brief introductory screen shown when an application is first loading. Here's a simple way to set it up:
Create a Splash Screen Component: First, create a new component for the splash screen.
// SplashScreen.jsx
import React from 'react';
import './SplashScreen.css'; // Optional: for custom styling
const SplashScreen = () => {
return (
<div className="splash-screen">
<h1>Loading...</h1>
{/* You can add an image, logo, or animation here */}
</div>
);
};
export default SplashScreen;
Styling the Splash Screen (optional): Add some CSS to style your splash screen.
/* SplashScreen.css */
.splash-screen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
width: 100vw;
position: fixed;
top: 0;
left: 0;
background-color: white; /* Change to your preference */
z-index: 1000;
}
Use the Splash Screen in App: Implement logic in your App component to show the splash screen while your app is loading.
// App.jsx
import React, { useState, useEffect } from 'react';
import SplashScreen from './SplashScreen';
// Import the rest of your components or pages
function App() {
const [loading, setLoading] = useState(true);
useEffect(() => {
const timer = setTimeout(() => {
setLoading(false);
}, 2000); // Adjust the timeout duration as needed
return () => clearTimeout(timer);
}, []);
if (loading) {
return <SplashScreen />;
}
return (
<div className="App">
{/* Your main application content */}
<h1>Hello, World!</h1>
</div>
);
}
export default App;
Run Your Application: Now, when you run your application with npm run dev, you should see the splash screen for a couple of seconds before your main app content is displayed.
setTimeout to simulate loading. In a real-world application, you would replace this with actual loading logic, such as fetching essential data or resources.This setup will help you add a simple yet effective splash screen to your React application. You can further customize it by adding additional animations, logos, or progress indicators as needed.
Not yet included in a published release.