Skip to content

Commit

Permalink
[add]useContext
Browse files Browse the repository at this point in the history
  • Loading branch information
wuping committed Feb 23, 2024
1 parent 9b3828c commit 91bf205
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/components/MainMenu/sliderConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ export const sliderConfig = [
label: 'hooks useEffect',
path: '/case/useEffect'
},
{
key: 33,
label: 'hooks useContext',
path: '/case/useContext'
},
]
}
]
Expand Down
9 changes: 9 additions & 0 deletions src/routers/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const Test = lazy(() => import('../views/Test'))
const Test1 = lazy(() => import('../views/Test/test1.tsx'))
const UseRef = lazy(() => import('../views/Case/useRef.tsx'))
const UseEffect = lazy(() => import('../views/Case/useEffect.tsx'))
const UseContext = lazy(() => import('../views/Case/useContext.tsx'))

import { defaultAuthLoad } from './auth'

Expand Down Expand Up @@ -90,6 +91,14 @@ const routes = [
role: ['admin']
}),
},
{
path: '/case/useContext',
element: defaultAuthLoad(lazyLoading(<UseContext />), {
title: 'hooks useContext',
role: ['admin']
}),
},

]
},
{
Expand Down
19 changes: 19 additions & 0 deletions src/views/Case/contextChild.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Context } from './useContext';

const contextChild = () => {
return (
<div>
<h1>Context Child</h1>
<Context.Consumer>
{
(value) => {
return <h2>{value.test}</h2>
}
}
</Context.Consumer>

</div>
);
}

export default contextChild;
25 changes: 25 additions & 0 deletions src/views/Case/useContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { createContext } from "react";
import ContextChild from "./contextChild";

type contextTheme = {
test: string | null
}

export const Context = createContext<contextTheme>({
test: null
});

const useContextCase = () => {
return (
<div>
useContextCase
<Context.Provider value={{
test: '111'
}}>
<ContextChild />
</Context.Provider>
</div>
)
}

export default useContextCase;

0 comments on commit 91bf205

Please sign in to comment.