将错误记录到控制台

Logging errors to console

我想记录我进入控制台的任何错误,当我尝试 运行 我的测试时收到此警告:

Warning: Functions are not valid as a React child. This may happen if you return a Component instead of from render. Or maybe you meant to call this function rather than return it. in ErrorDialog (at ErrorDialog.test.js:36) in WrapperComponent

这是我的组件:

import React from 'react';
import log from '../pathtologlevel';
import ErrorContext from '../pathtoErrorContext';

class Error extends React.Component {
 static type = ErrorContext;

 constructor(props, context) {
   super(props, context);
   this.state = { err: null };
   this.processError = this.processError.bind(this);
   this.list= [];
 }

 componentDidMount() {
   const errorService = this.context;
   errorService.attach((err) => {
     log.info('changing state');
     this.array.push(err);
     this.processError();
   });
 }

 processError() {
   const { err } = this.state;
   log.info('processNextError', this.array.length, err);
   if (!err && this.array.length > 0) {
     log.info('Displaying next error');
     this.setState({ err: this.array.shift() });
   }
 }

 render() {
   const { err } = this.state;
   log.info(err);
   if (err) {
     return this.processError;
   }
   return null;
 }
}

export default Error;

您正在返回函数 this.processError 而不是在渲染方法中调用它。只需调用函数 this.processError(),您应该不会收到任何警告。