- Email : dummy@gmail.com
- Password : Dummy@1234
- testing already there
- HMR
Lets start by setting up tailwind in the project Tailwind-webpage ==> get-started ==> Framework guide ==>create-react-app
npm install -D tailwindcss
npx tailwindcss init- Configure the tailwind.config.js
- modify index.css
rafce ==> react arrow function export component
formik best library to use form inside the react application because when form grows, it becomes tougher to do authentication, validation and handling error.
Certainly, here is a simplified explanation without code blocks:
-
useState:- Used to manage state in functional components.
- Causes a re-render when the state value changes.
-
useRef:- Used to persist values across renders without triggering re-renders.
- Does not cause the component to re-render when the ref's value changes.
- Useful for accessing and interacting with the DOM directly, storing mutable values, or persisting values between renders.
Use Cases:
- Use
useStatewhen you need to manage and display changing values in your UI. - Use
useRefwhen you need to store and access a mutable value that doesn't trigger re-renders. Common use cases include accessing and modifying DOM elements directly or persisting values across renders.
In summary, use useState for managing state that affects your component's rendering, and use useRef for persisting values without triggering re-renders, especially for non-reactive data like DOM references.
import React, { useRef } from 'react';
const SignInForm = () => {
// Create refs for email and password inputs
const emailRef = useRef(null);
const passwordRef = useRef(null);
const handleSignIn = () => {
// Access input values using ref.current.value
const email = emailRef.current.value;
const password = passwordRef.current.value;
// Now you can use email and password as needed (e.g., for authentication)
console.log('Email:', email);
console.log('Password:', password);
// Implement your sign-in logic here
};
return (
<div>
<form>
<label>
Email:
<input type="email" ref={emailRef} />
</label>
<br />
<label>
Password:
<input type="password" ref={passwordRef} />
</label>
<br />
<button type="button" onClick={handleSignIn}>
Sign In
</button>
</form>
</div>
);
};
export default SignInForm;Setting up a project named "NetflixGPT" on Firebase with Google Analytics, hosting, and user authentication involves several steps. Below is a step-by-step guide to help you achieve this:
- Go to the Firebase Console.
- Click on the "Add Project" button.
- Enter the project name as "NetflixGPT" and follow the on-screen instructions to complete the setup.
- After creating the project, click on the project name in the Firebase Console.
- In the left-hand menu, click on "Analytics."
- Follow the prompts to enable Google Analytics for your project.
- In the Firebase Console, navigate to "Authentication" from the left-hand menu.
- Click on the "Sign-in method" tab.
- Enable the sign-in providers you want to use (e.g., Email/Password, Google Sign-in).
- Follow the instructions to configure each sign-in provider.
Ensure you have the Firebase CLI installed on your machine. If not, you can install it using:
npm install -g firebase-tools- Open a terminal and navigate to your project folder.
- Run the following command to initialize Firebase:
firebase init- Choose the features you want to set up (Hosting is mandatory for web hosting).
- Run the following command to deploy your project to Firebase Hosting:
firebase deploy- After deployment, you will get a hosting URL. Access your project using that URL.
- In your web app code, add the Firebase configuration. You can get this configuration from the Firebase Console under Project settings.
// Initialize Firebase
var firebaseConfig = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_AUTH_DOMAIN",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_STORAGE_BUCKET",
messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
appId: "YOUR_APP_ID",
measurementId: "YOUR_MEASUREMENT_ID"
};
firebase.initializeApp(firebaseConfig);
firebase.analytics();- You may also need to install the Firebase SDK in your project:
npm install firebase- Use Firebase Authentication in your app based on your chosen authentication providers.
Firebase Analytics is integrated by default. You can view analytics data in the Firebase Console under the "Analytics" section.
That's it! You've now set up a Firebase project named "NetflixGPT" with Google Analytics, hosting, and user authentication. Make sure to refer to the Firebase documentation for detailed information and additional configuration options: Firebase Documentation.
- use modular API from firefox
import { getAuth, createUserWithEmailAndPassword } from "firebase/auth";
const auth = getAuth();
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// Signed up
const user = userCredential.user;
// ...
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
// ..
});- Add
const auth = getAuth()inside firebase.js - as soon as the user signin/sign-up we may need the data anywhere , so we will keep it now inside our redux store
- Once our user signins , sign-up, Sign-out we are required to add the userInfo to our userSlice, but dont want to do that repeatedly. Threfore this API comes in picture
- kind of like a event listener on user's activity
import { getAuth, onAuthStateChanged } from "firebase/auth";
const auth = getAuth();
onAuthStateChanged(auth, (user) => {
if (user) {
// User is signed in, see docs for a list of available properties
// https://firebase.google.com/docs/reference/js/auth.user
const uid = user.uid;
// ...
} else {
// User is signed out
// ...
}
});- when wants to re-direct to another page from one page
The
useNavigatehook is part of the React Router library and is used for navigating between different views or pages in a React application. It's particularly useful in components that are not directly rendered by aRoutecomponent but still need to perform navigation.
-
Import
useNavigate: Import theuseNavigatehook at the beginning of the file where you want to use it.import { useNavigate } from 'react-router-dom';
-
Use
useNavigatein your component: In your component, calluseNavigate()to get thenavigatefunction. This function can be used to programmatically navigate to different routes.const MyComponent = () => { const navigate = useNavigate(); const handleClick = () => { // Use the navigate function to go to a specific route navigate('/some-route'); }; return ( <div> <p>This is my component</p> <button onClick={handleClick}>Go to Some Route</button> </div> ); };
In the example above, when the button is clicked, the
handleClickfunction is called, and it uses thenavigatefunction to go to the/some-route. -
Dynamic Navigation: You can also use
navigatewith dynamic routes by providing parameters.const MyComponent = () => { const navigate = useNavigate(); const handleDynamicClick = (id) => { // Use navigate with dynamic route parameters navigate(`/dynamic-route/${id}`); }; return ( <div> <p>This is my component</p> <button onClick={() => handleDynamicClick(123)}>Go to Dynamic Route</button> </div> ); };
In this example, clicking the button will navigate to a dynamic route with the provided
id.
And that's how you can use the useNavigate hook in a React component to handle navigation. It provides a convenient way to navigate programmatically without relying on history prop drilling or other patterns.
- Each call is made two times inside the dev phase
- react checks for inconsistencies inside the data