让我们假设有一个Child
组件以这种方式呈现
<Child text="foo" action={this.bar} />
此处text
和action
是Child组件的两个道具。
但是如果没有提供给Child
组件的道具
<Child />
Child
组件会引发undefined
道具异常this.props.text is undefined
或类似事件。
即使在没有提供道具的情况下,呈现Child
组件的最佳做法是什么?
我是否必须始终在Child
组件中进行检查。
if (this.props.text !== undefined) && (this.props.action !== undefined)
//then render
答案 0 :(得分:0)
最佳做法是使用var database = firebase.database();
var rootRef = database.ref();
rootRef.orderByChild('email')
.equalTo('tomcruise@firebasemyapp.com')
.on('child_added')
.then(function(snapshot){
var result = snapshot.val();
//Do what you want with the result
});
包,例如:
PropTypes
这将在控制台中显示警告消息。
要使用它,您需要安装它:
import React, { Component } from 'react'
import PropTypes from 'prop-types'
class Person extends Component {
render () {
<h1>{this.props.name}</h1>
}
}
Person.propTyeps = {
name: PropTypes.string.isRequired
}
您可以找到更多文档here,希望它有所帮助。