用于反应的控制台日志记录? [英] Console logging for react?

查看:25
本文介绍了用于反应的控制台日志记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 React 的新手,我正在尝试为 Meteor 设置它,并将其他来源的东西拼凑在一起.这些其他来源之一为应用程序设置了控制台日志记录,但我将采用 ES6/JSX 方式,因此仅使用他们的代码对我不起作用(或者它似乎不起作用).

I'm super new to React and I'm trying to get it set up for Meteor and piecing stuff together from other sources too. One of these other sources set up console logging for the app, but I'm going the ES6/JSX way so just using their code wouldn't work for me (or it doesn't seem like it does).

我发现的一些用于日志记录的代码是

Some code I found for logging is

import Logger from 'simple-console-logger';
Logger.configure({level: 'debug'});

但我看到了这个错误

我也尝试使用 react-loggerreact-console-logger 无济于事.这是我为后者编写的代码,我认为它应该可以工作.

I also tried using react-logger and react-console-logger to no avail. Here's my code for the latter, which I believe should work.

import {Logger, ConsoleLogger} from 'react-console-logger';
const myLogger = new Logger();
export default class App extends Component {
    render() {
        myLogger.info('something witty');
    }
}

然而,myLogger.info('...') 正在调用具有它的 node_modules/react-console-logger/lib/Logger.js定义为

However, myLogger.info('...') is making a call to node_modules/react-console-logger/lib/Logger.js which has it defined as

而且 this.logger 是未定义的,虽然我看到上面已经定义了?

And this.logger is undefined, although I see it be defined above?

有谁知道我做错了什么?在我看来,这个库有问题,但也许这与我使用 JSX 文件而不是 js 有关系?

Does anyone know what I'm doing wrong? It looks to me like the library has it wrong, but maybe it has something to do with me using JSX files instead of js?

推荐答案

如果您只是在控制台登录后,我会这样做:

If you're just after console logging here's what I'd do:

export default class App extends Component {
  componentDidMount() {
    console.log('I was triggered during componentDidMount')
  }

  render() {
    console.log('I was triggered during render')
    return ( 
      <div> I am the App component </div>
    )
  }
}

应该不需要这些包只是为了进行控制台日志记录.

Shouldn't be any need for those packages just to do console logging.

这篇关于用于反应的控制台日志记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆