Commit 569c79d8 authored by Shen Chang's avatar Shen Chang

refactor(debug): Rewrite error message

parent 3fe0ceb4
......@@ -35,4 +35,4 @@ export default class Test extends React.Component {
<div>This is b.</div>
);
}
}
\ No newline at end of file
}
import React from 'react';
import Comment from './Comment';
import {LIFECYCLE, ICache, ICacheItem} from './Provider';
import {warn} from '../utils/debug';
import findDOMNodeByFiberNode from '../utils/findDOMNodeByFiberNode';
import createUniqueIdentification from '../utils/createUniqueIdentification';
......@@ -25,7 +26,7 @@ class Consumer extends React.PureComponent<IConsumerProps> {
super(props, ...args);
const {cache, setCache, children} = props;
if (!cache || !setCache) {
throw new Error('<KeepAlive> component must be in the <Provider> component.');
warn('[React Keep Alive] <KeepAlive> component must be in the <Provider> component.');
}
React.Children.only(children);
}
......
......@@ -3,6 +3,7 @@ import ReactDOM from 'react-dom';
import Comment from './Comment';
import KeepAliveContext from '../contexts/KeepAliveContext';
import createEventEmitter from '../utils/createEventEmitter';
import {warn} from '../utils/debug';
import createUniqueIdentification from '../utils/createUniqueIdentification';
import createStoreElement from '../utils/createStoreElement';
......@@ -88,7 +89,7 @@ export default class KeepAliveProvider extends React.PureComponent<IKeepAlivePro
const currentCache = cache[identification];
const key = currentCache && currentCache.key;
if (key && value.key && key !== (value.key as unknown)) {
throw new Error('Cached components have duplicates.');
warn('[React Keep Alive] Cached components have duplicates.');
}
if (!currentCache) {
keys.push(identification);
......
import {warn} from './debug';
type EventNames = string | string[];
type Listener = (...args: any) => void;
......@@ -17,7 +19,7 @@ export default function createEventEmitter() {
current = current[key];
}
if (!Array.isArray(current)) {
throw new Error('Access path error.');
warn('[React Keep Alive] Access path error.');
}
if (direction) {
current.unshift(listener);
......@@ -82,7 +84,7 @@ export default function createEventEmitter() {
function getEventNames(eventNames: EventNames): string[] {
if (!eventNames) {
throw new Error('Must exist event name.');
warn('[React Keep Alive] Must exist event name.');
}
if (typeof eventNames === 'string') {
eventNames = [eventNames];
......
type Warn = (message?: string) => void;
export let warn: Warn = () => undefined;
if (process.env.NODE_ENV !== 'production') {
/**
* Prints a warning in the console if it exists.
*
* @param {*} message
*/
warn = message => {
if (typeof console !== undefined && typeof console.error === 'function') {
console.error(message);
} else {
throw new Error(message);
}
};
}
......@@ -5,6 +5,7 @@ import Consumer from '../components/Consumer';
import {START_MOUNTING_DOM, LIFECYCLE} from '../components/Provider';
import md5 from './md5';
import noop from './noop';
import {warn} from './debug';
import getContextIdentificationByFiberNode from './getContextIdentificationByFiberNode';
import withIdentificationContextConsumer, {IIdentificationContextComponentProps} from './withIdentificationContextConsumer';
import withKeepAliveContextConsumer, {IKeepAliveContextComponentProps} from './withKeepAliveContextConsumer';
......@@ -65,7 +66,7 @@ export default function keepAliveDecorator({
const displayName = (name || getDisplayName(Component)) as any;
if (!displayName) {
throw new Error('Each component must have a name, which can be the component\'s displayName or name static property. You can also configure name when keepAlive decorates the component.');
warn('[React Keep Alive] Each component must have a name, which can be the component\'s displayName or name static property. You can also configure name when keepAlive decorates the component.');
}
Component.prototype.componentDidMount = function () {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment