Skip to content

Commit bf12a62

Browse files
committed
add: comments and examples of context
1 parent 456f260 commit bf12a62

File tree

8 files changed

+117
-4
lines changed

8 files changed

+117
-4
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import React from 'react'
2+
import PropTypes from 'prop-types'
3+
4+
class ProviderText extends React.Component{
5+
getChildContext() {
6+
return {
7+
text: this.props.text
8+
}
9+
}
10+
11+
render() {
12+
return <>
13+
<h3>ProviderText</h3>
14+
{this.props.children}
15+
</>
16+
}
17+
}
18+
ProviderText.childContextTypes = {
19+
text: PropTypes.string
20+
};
21+
22+
class ConsumerText extends React.Component{
23+
render() {
24+
return <div>ConsumerText: {this.context.text}</div>
25+
}
26+
}
27+
ConsumerText.contextTypes = {
28+
text: PropTypes.string
29+
}
30+
31+
class ProviderColor extends React.Component{
32+
getChildContext() {
33+
return {
34+
color: this.props.color
35+
}
36+
}
37+
38+
render() {
39+
return <>
40+
<h3 style={{ color: this.props.color }}>ProviderColor</h3>
41+
{this.props.children}
42+
</>
43+
}
44+
}
45+
ProviderColor.childContextTypes = {
46+
color: PropTypes.string
47+
};
48+
49+
class ConsumerColor extends React.Component{
50+
render() {
51+
const { color } = this.context
52+
return <div style={{ color }}>ConsumerColor: {color}</div>
53+
}
54+
}
55+
ConsumerColor.contextTypes = {
56+
color: PropTypes.string
57+
}
58+
59+
60+
class LagcyContext extends React.Component{
61+
state = {
62+
textValue: 'hello context',
63+
color: 'black'
64+
}
65+
render() {
66+
const { textValue, color } = this.state
67+
return <>
68+
<div style={{ marginTop: 80 }}>
69+
<h2>旧版context</h2>
70+
<p>
71+
颜色:
72+
<select
73+
name="color"
74+
id="color-selector"
75+
onChange={e => {
76+
this.setState({ color: e.target.value })
77+
}}
78+
>
79+
<option value="black">黑色</option>
80+
<option value="red">红色</option>
81+
<option value="green">绿色</option>
82+
</select>
83+
</p>
84+
<p>
85+
文字:
86+
<input
87+
type="text"
88+
defaultValue={textValue}
89+
onChange={e => {
90+
this.setState({ textValue: e.target.value })
91+
}}
92+
/>
93+
</p>
94+
</div>
95+
<ProviderText text={textValue}>
96+
<ProviderColor color={color}>
97+
<ConsumerColor/>
98+
<ConsumerText/>
99+
</ProviderColor>
100+
</ProviderText>
101+
</>
102+
}
103+
}
104+
export default LagcyContext

src/components/Context/LanguageContext/parent.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const { ZH_CN, EN } = LANGUAGE
66

77
const Parent = () => {
88
const [ language, setLanguage ] = useState(ZH_CN)
9-
console.log(LanguageContext);
109
return <div className={'language-context'}>
1110
<span>语言</span>
1211
<select

src/components/Context/ThemeContext/parent.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const { PURPLE, BLUE, RED } = THEME_COLOR
77
const Parent = () => {
88
const [ theme, setTheme ] = useState(PURPLE)
99
return <div className={'theme-context'}>
10+
<h2>新版context</h2>
1011
<span>主题</span>
1112
<select
1213
onChange={e => {

src/components/Context/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import React from 'react'
22
import ThemeParent from './ThemeContext/parent'
33
import LanguageParent from './LanguageContext/parent'
4+
import LagcyContext from './LagcyContext/index'
45
import './index.css'
56

67
const ContextDemo = () => {
78
return <div className={'context-demo'}>
89
<ThemeParent/>
910
<LanguageParent/>
11+
<LagcyContext/>
1012
</div>
1113
}
1214
export default ContextDemo

src/react/v17/react-reconciler/src/ReactFiberClassComponent.old.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ function constructClassInstance(
626626
context = readContext((contextType: any));
627627
} else if (!disableLegacyContext) {
628628
unmaskedContext = getUnmaskedContext(workInProgress, ctor, true);
629+
console.log('unmaskedContext', unmaskedContext);
629630
const contextTypes = ctor.contextTypes;
630631
isLegacyContextConsumer =
631632
contextTypes !== null && contextTypes !== undefined;

src/react/v17/react-reconciler/src/ReactFiberContext.old.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ function getUnmaskedContext(
5454
// we may have already pushed its own child context on the stack. A context
5555
// provider should not "see" its own child context. Therefore we read the
5656
// previous (parent) context instead for a context provider.
57+
console.log('previousContext', previousContext);
5758
return previousContext;
5859
}
5960
return contextStackCursor.current;

src/react/v17/react-reconciler/src/ReactFiberHostContext.old.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ function pushHostContext(fiber: Fiber): void {
8080
);
8181
const context: HostContext = requiredContext(contextStackCursor.current);
8282
const nextContext = getChildHostContext(context, fiber.type, rootInstance);
83-
8483
// Don't push this Fiber's context unless it's unique.
8584
if (context === nextContext) {
8685
return;

src/react/v17/react-reconciler/src/ReactFiberStack.old.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,20 @@ function pop<T>(cursor: StackCursor<T>, fiber: Fiber): void {
5757
}
5858

5959
function push<T>(cursor: StackCursor<T>, value: T, fiber: Fiber): void {
60+
console.log('value', value);
6061
index++;
61-
62+
/*
63+
* [ { theme: 'red' }, false ]
64+
*
65+
* [ emptyContext, contextValue ]
66+
*
67+
* */
6268
valueStack[index] = cursor.current;
6369

6470
if (__DEV__) {
6571
fiberStack[index] = fiber;
6672
}
67-
73+
// console.log('valueStack', valueStack);
6874
cursor.current = value;
6975
}
7076

0 commit comments

Comments
 (0)