-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
161 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8" /> | ||
<link rel="icon" type="image/svg+xml" href="/vite.svg" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>context测试</title> | ||
</head> | ||
|
||
<body> | ||
<div id="root"></div> | ||
<script type="module" src="main.tsx"></script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useState, createContext, useContext } from 'react'; | ||
import ReactDOM from 'react-dom/client'; | ||
|
||
const ctxA = createContext('deafult A'); | ||
const ctxB = createContext('default B'); | ||
|
||
function App() { | ||
return ( | ||
<ctxA.Provider value={'A0'}> | ||
<ctxB.Provider value={'B0'}> | ||
<ctxA.Provider value={'A1'}> | ||
<Cpn /> | ||
</ctxA.Provider> | ||
</ctxB.Provider> | ||
<Cpn /> | ||
</ctxA.Provider> | ||
); | ||
} | ||
|
||
function Cpn() { | ||
const a = useContext(ctxA); | ||
const b = useContext(ctxB); | ||
return ( | ||
<div> | ||
A: {a} B: {b} | ||
</div> | ||
); | ||
} | ||
|
||
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render( | ||
<App /> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/// <reference types="vite/client" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { ReactContext } from 'shared/ReactTypes'; | ||
|
||
let prevContextValue: any = null; | ||
const prevContextValueStack: any[] = []; | ||
|
||
export function pushProvider<T>(context: ReactContext<T>, newValue: T) { | ||
prevContextValueStack.push(prevContextValue); | ||
|
||
prevContextValue = context._currentValue; | ||
context._currentValue = newValue; | ||
} | ||
|
||
export function popProvider<T>(context: ReactContext<T>) { | ||
context._currentValue = prevContextValue; | ||
|
||
prevContextValue = prevContextValueStack.pop(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { REACT_CONTEXT_TYPE, REACT_PROVIDER_TYPE } from 'shared/ReactSymbols'; | ||
import { ReactContext } from 'shared/ReactTypes'; | ||
|
||
export function createContext<T>(defaultValue: T): ReactContext<T> { | ||
const context: ReactContext<T> = { | ||
$$typeof: REACT_CONTEXT_TYPE, | ||
Provider: null, | ||
_currentValue: defaultValue | ||
}; | ||
context.Provider = { | ||
$$typeof: REACT_PROVIDER_TYPE, | ||
_context: context | ||
}; | ||
return context; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters