简体   繁体   English

如何在React中console.log道具?

[英]How to console.log props in React?

I'm new to React and have been thrown in the deep end with a task. 我是React的新手,被深深地吸引着一个任务。

I must adjust the data for a date props. 我必须调整日期道具的数据。 This looks (as far as I can tell) like: 据我所知,这看起来像:

form.defaultProps = {
  conf: {},
  minDate: new Date(),
  maxDate: new Date(new Date().setFullYear(new Date().getFullYear() + 2)),
};

I want to test if maxDate is the prop I am after. 我想测试maxDate是否是我想要的道具。 To do that I want to console.log (or equivalent) its output. 为此,我要console.log (或等效命令)输出。

Would anyone know how I should be doing this? 有人知道我应该怎么做吗?

You should be able to just log it directly: 您应该可以直接将其记录:

console.log(form.defaultProps);

Most likely, you might want to this within the form component you are creating by calling console.log(this.props) . 最有可能的是,您可能希望通过调用console.log(this.props)在要创建的form组件中执行此操作。

As others are saying, you can simple do console.log(this.props) somewhere. 就像其他人所说的,您可以在某个地方简单地做console.log(this.props) I usually put it in the render() function so that every time there's an update to props or state, it will be logged again. 我通常将其放在render()函数中,以便每次对道具或状态进行更新时,都将再次记录该更新。

But if your sole purpose is to examine the props values for development, I'd recommend you to use React Devtools extension . 但是,如果您唯一的目的是检查用于开发的props值,则建议您使用React Devtools extension

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM