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>);
}
}
這是在工程?