File tree Expand file tree Collapse file tree 6 files changed +35
-48
lines changed Expand file tree Collapse file tree 6 files changed +35
-48
lines changed Original file line number Diff line number Diff line change
1
+ "use client"
2
+ import React , { createContext } from 'react'
3
+ export const MyContext = createContext ( )
4
+ const Context = ( { children } ) => {
5
+ const username = "Code with dp"
6
+ return (
7
+ < div > < MyContext . Provider value = { username } >
8
+ { children }
9
+ </ MyContext . Provider > </ div >
10
+ )
11
+ }
12
+
13
+ export default Context
Original file line number Diff line number Diff line change
1
+ import React , { useContext } from 'react'
2
+ import { MyContext } from '@/Helper/Context'
3
+ const Header = ( ) => {
4
+ const username = useContext ( MyContext )
5
+ return (
6
+ < div > < h1 > The name of this webiste is { username } </ h1 > </ div >
7
+ )
8
+ }
9
+
10
+ export default Header
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 3
3
@tailwind utilities;
4
4
5
5
body {
6
- padding : 50 px ;
6
+ padding : 5 px ;
7
7
}
Original file line number Diff line number Diff line change
1
+ import MyContext from "@/Helper/Context" ;
1
2
import "./globals.css" ;
2
3
export const metadata = {
3
4
title : "React Advanced" ,
@@ -7,7 +8,9 @@ export const metadata = {
7
8
export default function RootLayout ( { children } ) {
8
9
return (
9
10
< html lang = "en" >
10
- < body suppressContentEditableWarning > { children } </ body >
11
+ < body suppressContentEditableWarning >
12
+ < MyContext > { children } </ MyContext >
13
+ </ body >
11
14
</ html >
12
15
) ;
13
16
}
Original file line number Diff line number Diff line change 1
1
"use client"
2
- import axios from 'axios'
3
- import Link from 'next/link'
4
- import React , { useEffect , useState } from 'react'
5
-
2
+ import React , { useContext } from 'react'
3
+ import { MyContext } from '@/Helper/Context'
4
+ import Header from './Components/Header'
6
5
const page = ( ) => {
7
-
8
- const [ users , setusers ] = useState ( [ ] )
9
-
10
- const getUsers = async ( ) => {
11
- const { data } = await axios . get ( "https://jsonplaceholder.typicode.com/users" )
12
- setusers ( data )
13
- // console.log(data)
14
- }
15
-
16
- useEffect ( ( ) => {
17
- getUsers ( )
18
- } , [ ] )
19
-
6
+ const user = useContext ( MyContext )
20
7
return (
21
8
< >
22
- < button onClick = { getUsers } className = 'text-white font-bold px-4 py-2 rounded bg-green-600' > Get Data</ button >
23
- < div className = 'bg-slate-100 p-8 mt-5' >
24
- < ul > { users . map ( ( e , id ) => {
25
- return < li key = { id } > { e . username } ---< Link href = { `${ e . id } ` } > Explore</ Link > </ li >
26
- } ) } </ ul >
27
- </ div >
28
- </ >
9
+ < Header />
10
+ < div className = 'text-black' > { user }
11
+ </ div > </ >
29
12
)
30
13
}
31
14
You can’t perform that action at this time.
0 commit comments