关于invariant的问题

来源:2-4 react-component

qq_crusader_1

2019-11-23

function invariant(condition, format, a, b, c, d, e, f) {
  validateFormat(format);
  console.log('invariant', condition, format, a,b,c,d,e,f)
  if (!condition) {
    var error = void 0;
    if (format === undefined) {
      error = new Error('Minified exception occurred; use the non-minified dev environment ' + 'for the full error message and additional helpful warnings.');
    } else {
      var args = [a, b, c, d, e, f];
      var argIndex = 0;
      // /%s/g全局匹配字符串中'%s'字符串,第二个参数可以是字面值也可以是一个函数,若是字面值,那么它会
      // 全局替换成这个字面值,若是一个函数的话,那么它会递归正则里面的每个值,比如:format中有3个%s,那么会
      // 对这3个%s进行单独的值替换
      error = new Error(format.replace(/%s/g, function () {
        return args[argIndex++];
      }));
      error.name = 'Invariant Violation';
    }

    error.framesToPop = 1; // we don't care about invariant's own frame
    throw error;
  }
}

validateFormat在format为空的时候报错,那么下面为何还format==undefined,这个时候还起作用吗?有必要再做一次判断吗?
还有framesToPop是干啥的?

写回答

1回答

Jokcy

2019-11-24

format==undefined是啥,能把调用invariant的代码给出么

0
1
qq_crusader_1
invariant( typeof partialState === 'object' || typeof partialState === 'function' || partialState == null, 'setState(...): takes an object of state variables to update or a ' + 'function which returns an object of state variables.', );
2019-11-30
共1条回复

React源码深度解析 高级前端工程师必备技能

掌握React源码,让你的开发水平没有上限,更不惧前端未来的到来

1749 学习 · 336 问题

查看课程