注意:结合Context , Provider,Consumer的使用说明阅读,会更有收获
https://www.yuque.com/lijunyang/dk90s4/akf6xn#RrPJp
<!-- /dist/index.html -->
<!doctype html>
<html>
<head>
<title>react</title>
<script src="./react.js"></script>
<script src="./react-dom.js"></script>
</head>
<body>
<div id="root">
</div>
<script>
debugger;
var context = React.createContext('默认值');
var contextProvider = context.Provider;
var contextConsumer = context.Consumer;
var Son = (function (Component) {
Mod.prototype = Object.create(Component.prototype);
Mod.prototype.constructor = Mod;
Mod.__proto__ = Component;
function Mod() {
debugger;
var _this;
_this = Component.apply(this, arguments) || this;
return _this;
}
Mod.prototype.render = function () {
const self = this;
console.log('self', self);
return React.createElement(
'div',
{
onClick: function () {
self.context.rename('999');
}
},
self.context.value
);
}
return Mod;
}(React.Component))
var ShouldFalse = (function (Component) {
Mod.prototype = Object.create(Component.prototype);
Mod.prototype.constructor = Mod;
Mod.__proto__ = Component;
function Mod() {
var _this;
_this = Component.apply(this, arguments) || this;
_this.shouldComponentUpdate = function () {
// 接受两个参数nextProps,nextState
console.log('shouldComponentUpdate', arguments)
return false; // context子组件可以不受到shouldComponentUpdate的影响
}
return _this;
}
Mod.prototype.render = function () {
const self = this;
console.log('self', self);
return React.createElement(
'div',
{
onClick: function () {
self.context.rename('888');
}
},
self.context.value
);
}
return Mod;
}(React.Component))
// 注意,这个是关键,没有的话,就不能拿到Context了
Son.contextType = context;
ShouldFalse.contextType = context;
var Parent = (function (Component) {
Mod.prototype = Object.create(Component.prototype);
Mod.prototype.constructor = Mod;
Mod.__proto__ = Component;
function Mod() {
var _this;
_this = Component.apply(this, arguments) || this;
_this.state = {
name: 'TestMod',
errorFlag: false,
};
_this.componentDidMount = function () {
// 没参数
console.log('componentDidMount', arguments)
}
_this.shouldComponentUpdate = function () {
// 接受两个参数nextProps,nextState
console.log('shouldComponentUpdate', arguments)
return true;
}
_this.getSnapshotBeforeUpdate = function () {
// 接受2个参数,return的值作为componentDidUpdate的第三个参数
console.log('getSnapShotBeforeUpdate', arguments)
return { a: 1 };
}
_this.componentDidUpdate = function () {
// 接受3个参数,prevProps,prevState
console.log('componentDidUpdate', arguments)
}
_this.componentDidCatch = function (error, info) {
// 子组件的render发生异常,才会被执行的生命周期
console.log('Parent componentDidCatch', arguments)
_this.setState({
errorFlag: true,
})
}
return _this;
}
var count = 0;
Mod.getDerivedStateFromProps = function () {
if (count === 0) {
console.log('初始化 getDerivedStateFromProps')
} else {
console.log('变更期 getDerivedStateFromProps')
}
return null;
}
Mod.prototype.render = function () {
if (count === 0) {
console.log("初始化 render")
count = 1;
} else {
console.log("变更起 render")
}
const self = this;
var errorFlag = this.state.errorFlag;
return React.createElement(
'div',
{
key: 'Parent',
id: 'Parent',
},
React.createElement(
contextProvider,
{
value: {
value: self.state.name,
rename: function rename(name) {
self.setState({
name: name
});
}
}
},
React.createElement(Son, null),
React.createElement(ShouldFalse, null),
React.createElement('div', null,
React.createElement(function (props, context) {
console.log('arguments1', arguments); // 它没有props,context
return React.createElement(Son, null); // 它拿到了Context
})
),
React.createElement(contextConsumer, null, function (props) {
console.log('arguments2', arguments); // 它没有context,但是它拿到了Props
return React.createElement('div', null, props.value)
})
),
React.createElement(contextProvider, null, // 拿不到默认的value
React.createElement(contextConsumer, null, function () { // 拿不到默认的value
console.log('arguments3', arguments);
return React.createElement('div', null, '111')
})
),
React.createElement(contextProvider, null, // 它什么都没拿到
React.createElement(function () {
console.log('arguments4', arguments);
return React.createElement('div', null, '111')
})
),
React.createElement(contextConsumer, null, function (props) { // 只有它才能拿到默认的value
console.log('arguments5', arguments);
return React.createElement('div', null, props)
})
);
}
return Mod;
}(React.Component))
var reactElement = React.createElement(Parent, null);
ReactDOM.render(reactElement, document.getElementById('root'))
</script>
</body>
</html>
function createContext (defaultValue, calculateChangedBits) {
if (calculateChangedBits === undefined) {
calculateChangedBits = null;
} else {
{
!(calculateChangedBits === null || typeof calculateChangedBits === 'function') ? warningWithoutStack$1(false, 'createContext: Expected the optional second argument to be a ' + 'function. Instead received: %s', calculateChangedBits) : void 0;
}
}
var context = {
$$typeof: REACT_CONTEXT_TYPE,
_calculateChangedBits: calculateChangedBits,
_currentValue: defaultValue,
_currentValue2: defaultValue,
_threadCount: 0,
Provider: null,
Consumer: null
};
context.Provider = {
$$typeof: REACT_PROVIDER_TYPE,
_context: context
};
var hasWarnedAboutUsingNestedContextConsumers = false;
var hasWarnedAboutUsingConsumerProvider = false;
{
var Consumer = {
$$typeof: REACT_CONTEXT_TYPE,
_context: context,
_calculateChangedBits: context._calculateChangedBits
};
Object.defineProperties(Consumer, {
Provider: {
get: function () {
if (!hasWarnedAboutUsingConsumerProvider) {
hasWarnedAboutUsingConsumerProvider = true;
warning$1(false, 'Rendering <Context.Consumer.Provider> is not supported and will be removed in ' + 'a future major release. Did you mean to render <Context.Provider> instead?');
}
return context.Provider;
},
set: function (_Provider) {
context.Provider = _Provider;
}
},
_currentValue: {
get: function () {
return context._currentValue;
},
set: function (_currentValue) {
context._currentValue = _currentValue;
}
},
_currentValue2: {
get: function () {
return context._currentValue2;
},
set: function (_currentValue2) {
context._currentValue2 = _currentValue2;
}
},
_threadCount: {
get: function () {
return context._threadCount;
},
set: function (_threadCount) {
context._threadCount = _threadCount;
}
},
Consumer: {
get: function () {
if (!hasWarnedAboutUsingNestedContextConsumers) {
hasWarnedAboutUsingNestedContextConsumers = true;
warning$1(false, 'Rendering <Context.Consumer.Consumer> is not supported and will be removed in ' + 'a future major release. Did you mean to render <Context.Consumer> instead?');
}
return context.Consumer;
}
}
});
context.Consumer = Consumer;
}
{
context._currentRenderer = null;
context._currentRenderer2 = null;
}
return context;
}