0
在构建了好几个React组件,添加propTypes,编写测试以及添加偶尔的默认道具之后,我仍然在质疑安全性太高。在涉及Flow类型之后,我开始感觉到在尝试呈现内容之前需要添加更多安全检查以确保所有预期数据都存在。我应该在我的私人组件中包含多少安全逻辑?
我可以看到额外安全的一个好处是确保组件始终正确呈现,即使API失败。另一方面,失败的API可能会因失败而更好地处理。这些组件非常内部,它们的用途非常明确,并且易于追踪。
电流分量:
class Example extends Component {
render() {
return this.props.items.slice(4)
.map(item => <div>{item.name}</div>);
}
}
随着安全:
class Example extends Component {
render() {
return this.props.items &&
this.props.items.length &&
this.props.items.length > 3 &&
this.props.items.slice(4)
.map(item => <div>{item.name}</div>);
}
}
这是在工程?