├── vite.config.js
├── src
├── Hooks
│ ├── Context
│ │ ├── Comp2.jsx
│ │ ├── Comp1.jsx
│ │ ├── Comp3.jsx
│ │ └── UseContextHook2.jsx
│ ├── CallBack
│ │ ├── Child.jsx
│ │ └── UseCallBack2.jsx
│ ├── UseMemo2.jsx
│ └── UseStateHook.jsx
├── main.jsx
├── App.jsx
├── App.css
├── index.css
└── assets
│ └── react.svg
├── .gitignore
├── index.html
├── README.md
├── .eslintrc.cjs
├── package.json
└── public
└── vite.svg
/vite.config.js:
--------------------------------------------------------------------------------
1 | import { defineConfig } from 'vite'
2 | import react from '@vitejs/plugin-react'
3 |
4 | // https://vitejs.dev/config/
5 | export default defineConfig({
6 | plugins: [react()],
7 | })
8 |
--------------------------------------------------------------------------------
/src/Hooks/Context/Comp2.jsx:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import Comp3 from './Comp3'
3 |
4 | const Comp2 = () => {
5 | return (
6 |
7 |
8 |
9 | )
10 | }
11 |
12 | export default Comp2
--------------------------------------------------------------------------------
/src/Hooks/Context/Comp1.jsx:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import Comp2 from './Comp2'
3 |
4 | const Comp1 = () => {
5 | return (
6 |
7 |
8 |
9 | )
10 | }
11 |
12 | export default Comp1
--------------------------------------------------------------------------------
/src/main.jsx:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import ReactDOM from 'react-dom/client'
3 | import App from './App.jsx'
4 |
5 |
6 | ReactDOM.createRoot(document.getElementById('root')).render(
7 |
8 |
9 | ,
10 | )
11 |
--------------------------------------------------------------------------------
/src/Hooks/CallBack/Child.jsx:
--------------------------------------------------------------------------------
1 | import React,{memo} from 'react'
2 |
3 | function Child({countTwo,setCountTwo}) {
4 | console.log("Rendered Child")
5 | return (
6 | Child
7 |
8 | )
9 | }
10 | //first solution is memo Api
11 | export default memo(Child)
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | logs
3 | *.log
4 | npm-debug.log*
5 | yarn-debug.log*
6 | yarn-error.log*
7 | pnpm-debug.log*
8 | lerna-debug.log*
9 |
10 | node_modules
11 | dist
12 | dist-ssr
13 | *.local
14 |
15 | # Editor directories and files
16 | .vscode/*
17 | !.vscode/extensions.json
18 | .idea
19 | .DS_Store
20 | *.suo
21 | *.ntvs*
22 | *.njsproj
23 | *.sln
24 | *.sw?
25 |
--------------------------------------------------------------------------------
/src/Hooks/Context/Comp3.jsx:
--------------------------------------------------------------------------------
1 | import React, { useContext } from 'react'
2 | import { nameContext, nameContext2 } from './UseContextHook2'
3 |
4 | function Comp3() {
5 | const name = useContext(nameContext)
6 | const name2 = useContext(nameContext2)
7 | return (
8 |
9 |
10 |
{name}
11 | {name2}
12 |
13 | )
14 | }
15 |
16 | export default Comp3
--------------------------------------------------------------------------------
/src/App.jsx:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import UseContextHook2 from './Hooks/Context/UseContextHook2'
3 | // import UseMemo2 from './Hooks/UseMemo2.jsx'
4 | // import UseCallBack2 from './Hooks/CallBack/UseCallBack2'
5 | function App() {
6 | return (
7 |
8 |
9 | {/* */}
10 | {/* */}
11 |
12 | )
13 | }
14 |
15 | export default App
--------------------------------------------------------------------------------
/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Vite + React
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # React + Vite
2 |
3 | This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
4 |
5 | Currently, two official plugins are available:
6 |
7 | - [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
8 | - [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
9 |
--------------------------------------------------------------------------------
/.eslintrc.cjs:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | root: true,
3 | env: { browser: true, es2020: true },
4 | extends: [
5 | 'eslint:recommended',
6 | 'plugin:react/recommended',
7 | 'plugin:react/jsx-runtime',
8 | 'plugin:react-hooks/recommended',
9 | ],
10 | ignorePatterns: ['dist', '.eslintrc.cjs'],
11 | parserOptions: { ecmaVersion: 'latest', sourceType: 'module' },
12 | settings: { react: { version: '18.2' } },
13 | plugins: ['react-refresh'],
14 | rules: {
15 | 'react/jsx-no-target-blank': 'off',
16 | 'react-refresh/only-export-components': [
17 | 'warn',
18 | { allowConstantExport: true },
19 | ],
20 | },
21 | }
22 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "hooks",
3 | "private": true,
4 | "version": "0.0.0",
5 | "type": "module",
6 | "scripts": {
7 | "dev": "vite",
8 | "build": "vite build",
9 | "lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0",
10 | "preview": "vite preview"
11 | },
12 | "dependencies": {
13 | "react": "^18.2.0",
14 | "react-dom": "^18.2.0"
15 | },
16 | "devDependencies": {
17 | "@types/react": "^18.2.56",
18 | "@types/react-dom": "^18.2.19",
19 | "@vitejs/plugin-react": "^4.2.1",
20 | "eslint": "^8.56.0",
21 | "eslint-plugin-react": "^7.33.2",
22 | "eslint-plugin-react-hooks": "^4.6.0",
23 | "eslint-plugin-react-refresh": "^0.4.5",
24 | "vite": "^5.1.4"
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/App.css:
--------------------------------------------------------------------------------
1 | #root {
2 | max-width: 1280px;
3 | margin: 0 auto;
4 | padding: 2rem;
5 | text-align: center;
6 | }
7 |
8 | .logo {
9 | height: 6em;
10 | padding: 1.5em;
11 | will-change: filter;
12 | transition: filter 300ms;
13 | }
14 | .logo:hover {
15 | filter: drop-shadow(0 0 2em #646cffaa);
16 | }
17 | .logo.react:hover {
18 | filter: drop-shadow(0 0 2em #61dafbaa);
19 | }
20 |
21 | @keyframes logo-spin {
22 | from {
23 | transform: rotate(0deg);
24 | }
25 | to {
26 | transform: rotate(360deg);
27 | }
28 | }
29 |
30 | @media (prefers-reduced-motion: no-preference) {
31 | a:nth-of-type(2) .logo {
32 | animation: logo-spin infinite 20s linear;
33 | }
34 | }
35 |
36 | .card {
37 | padding: 2em;
38 | }
39 |
40 | .read-the-docs {
41 | color: #888;
42 | }
43 |
--------------------------------------------------------------------------------
/src/Hooks/Context/UseContextHook2.jsx:
--------------------------------------------------------------------------------
1 | import React, { createContext } from 'react'
2 | import Comp3 from './Comp3'
3 | export const nameContext = createContext()
4 | export const nameContext2 = createContext()
5 | function UseContextHook2() {
6 | //we can avoid props drilling then use context api
7 | //context api global context make
8 | //It can be used together with the useState Hook to share state between deeply nested components more easily than with useState alone.
9 | return (
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 | )
20 | }
21 |
22 | export default UseContextHook2
--------------------------------------------------------------------------------
/src/Hooks/CallBack/UseCallBack2.jsx:
--------------------------------------------------------------------------------
1 | import React,{useCallback, useState} from 'react'
2 | import Child from './Child'
3 | function UseCallBack2() {
4 | //when click btn child component call why
5 | //first solution is memo Api
6 | //but memo api not work whe child component pass a props
7 | //we use a hook useCallback when props pass
8 | const [count, setCount] = useState(0)
9 |
10 | const [countTwo,setCountTwo] = useState([])
11 |
12 | const incrementOne = () =>{
13 | setCount(count + 1)
14 | }
15 | const fn = useCallback(() =>{
16 | console.log("hello")
17 | },[countTwo])
18 | return (
19 |
20 |
21 |
22 | {count}
23 |
24 | )
25 | }
26 |
27 | export default UseCallBack2
--------------------------------------------------------------------------------
/src/Hooks/UseMemo2.jsx:
--------------------------------------------------------------------------------
1 | import React, { useMemo, useState } from 'react'
2 | //why One btn impact or effect Two btn but both or separated but one btn impact two and loading
3 | function UseMemo2() {
4 | const [count, setCount] = useState(0)
5 | const [countTwo,setCountTwo] = useState(0)
6 |
7 | const incrementOne = () =>{
8 | setCount(count + 1)
9 | }
10 | const incrementTwo = () =>{
11 | setCountTwo(countTwo + 1)
12 | }
13 |
14 | const isEven = useMemo(() => {
15 | console.warn("print One");
16 | let i = 0;
17 | while (i < 2000000000) i++;
18 | return count % 2 === 0;
19 | }, [count]);
20 | // const isEven =()=> {
21 | // console.warn("print")
22 | // let i=0;
23 | // while (i<2000000000) i++;
24 | // return count%2===0;
25 |
26 | // }
27 | return (
28 |
29 |
32 | {count}
33 | {isEven ? "Even": "odd"}
34 | {/* {isEven() ? "Even": "odd"} */}
35 |
38 | {countTwo}
39 |
40 | )
41 | }
42 |
43 | export default UseMemo2
--------------------------------------------------------------------------------
/src/Hooks/UseStateHook.jsx:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import { useState } from 'react'
3 | function UseStateHook() {
4 | // Declaring a state variable is as simple as calling useState with some initial state value, like so: useState(initialStateValue).
5 |
6 |
7 | const [age, setAge] = useState(19)
8 | const handleClick = () =>{
9 | console.log('Button Clicked')
10 | setAge(age + 1)
11 | }
12 |
13 | const [ageNew, setAgeNew] = useState(19)
14 | const [siblingsNum, setSiblingsNum] =
15 | useState(10)
16 |
17 | const handleAge = () => setAgeNew(age + 1)
18 | const handleSiblingsNum = () =>
19 | setSiblingsNum(siblingsNum + 1)
20 | return (
21 |
22 | Today I am {age} Years of Age
23 |
24 |
25 |
26 |
27 |
28 |
Today I am {ageNew} Years of Age
29 |
I have {siblingsNum} siblings
30 |
31 |
32 |
35 |
38 |
39 |
40 | )
41 | }
42 |
43 | export default UseStateHook
--------------------------------------------------------------------------------
/public/vite.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/src/index.css:
--------------------------------------------------------------------------------
1 | :root {
2 | font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
3 | line-height: 1.5;
4 | font-weight: 400;
5 |
6 | color-scheme: light dark;
7 | color: rgba(255, 255, 255, 0.87);
8 | background-color: #242424;
9 |
10 | font-synthesis: none;
11 | text-rendering: optimizeLegibility;
12 | -webkit-font-smoothing: antialiased;
13 | -moz-osx-font-smoothing: grayscale;
14 | }
15 |
16 | a {
17 | font-weight: 500;
18 | color: #646cff;
19 | text-decoration: inherit;
20 | }
21 | a:hover {
22 | color: #535bf2;
23 | }
24 |
25 | body {
26 | margin: 0;
27 | display: flex;
28 | place-items: center;
29 | min-width: 320px;
30 | min-height: 100vh;
31 | }
32 |
33 | h1 {
34 | font-size: 3.2em;
35 | line-height: 1.1;
36 | }
37 |
38 | button {
39 | border-radius: 8px;
40 | border: 1px solid transparent;
41 | padding: 0.6em 1.2em;
42 | font-size: 1em;
43 | font-weight: 500;
44 | font-family: inherit;
45 | background-color: #1a1a1a;
46 | cursor: pointer;
47 | transition: border-color 0.25s;
48 | }
49 | button:hover {
50 | border-color: #646cff;
51 | }
52 | button:focus,
53 | button:focus-visible {
54 | outline: 4px auto -webkit-focus-ring-color;
55 | }
56 |
57 | @media (prefers-color-scheme: light) {
58 | :root {
59 | color: #213547;
60 | background-color: #ffffff;
61 | }
62 | a:hover {
63 | color: #747bff;
64 | }
65 | button {
66 | background-color: #f9f9f9;
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/src/assets/react.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------