2016-11-17 115 views
15

我对React非常陌生,我正试图让它成立Meteor,并将其他来源的东西拼凑在一起。其中一个来源为应用程序设置了控制台日志记录,但是我将采用ES6/JSX方式,所以只是使用他们的代码对我来说不起作用(或者它看起来不像)。控制台日志记录反应?

一些代码,我发现记录是

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

,但我看到这个错误 cannot find module './dumy'

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

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呼叫已它定义为

picture of code since copy-paste doesn't work

而且this.logger是不确定的,但我看到它被上面定义的?

有谁知道我在做什么错?它看起来像我的图书馆有错误,但也许它与我使用JSX文件而不是js有关?

+0

你需要登录只是为了帮助调试/写你的代码,或者这是一个更永久固定的? – patrick

+0

只是为了帮助调试。我可以没有,但如果我甚至不能让控制台日志记录工作,我不知道我可以得到任何工作... – adinutzyc21

+1

没有必要重新发明轮子。这是解释所有问题的最佳链接。 https://codeburst.io/react-native-debugging-tools-3a24e4e40e4 –

回答

23

如果你只是控制台登录后在这里就是我想要做的:

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> 
    ) 
    } 
} 

应该是没有什么需要这些软件包只是做控制台日志记录。

+4

你甚至可以添加样式到你的console.log消息。试试这个'console.log('%c color message','color:#f0c002')' – Khang

+0

毫无疑问,这是一个很棒的答案,但我得到了一个警告:'意外的控制台声明无控制台' – adi

+2

@adi由ESLint引起 - http://eslint.org/docs/rules/no-console – patrick

10

这里有一些更多的控制台登录 “PRO提示”:

console.table

var animals = [ 
    { animal: 'Horse', name: 'Henry', age: 43 }, 
    { animal: 'Dog', name: 'Fred', age: 13 }, 
    { animal: 'Cat', name: 'Frodo', age: 18 } 
]; 

console.table(animals); 

console.table

console.trace

显示您调用堆栈为领导到控制台。

console.trace

你甚至可以自定义您的控制台,使他们脱颖而出

console.todo = function(msg) { 
    console.log(‘ % c % s % s % s‘, ‘color: yellow; background - color: black;’, ‘–‘, msg, ‘–‘); 
} 

console.important = function(msg) { 
    console.log(‘ % c % s % s % s’, ‘color: brown; font - weight: bold; text - decoration: underline;’, ‘–‘, msg, ‘–‘); 
} 

console.todo(“This is something that’ s need to be fixed”); 
console.important(‘This is an important message’); 

console.todo

如果你真的想要提升等级不限制你的自我到控制台的语句。

这是一篇关于如何将铬调试器直接集成到代码编辑器的好文章!

https://hackernoon.com/debugging-react-like-a-champ-with-vscode-66281760037

相关问题