1
假設input
有value={this.state.searchValue}
。在使用setState清除它之前,您應該檢查輸入是否爲空?
onClose =() => {
this.setState({searchValue: ''},() => {
this.search();
});
}
這是浪費,如果容器已關閉,而字符串已空?
應該這樣做嗎?
onClose =() => {
if (!isEmpty(this.state.searchValue)) {
this.setState({searchValue: ''},() => {
this.search();
});
}
}
如果這是Redux
狀態?
onClose =() => {
if (this.props.inputValue) {
this.props.clearInput();
}
}
我覺得Redux
是有點小聰明,並做了shouldComponentUpdate
給你,所以它會實現this.props.inputValue === ''
和nextProps.inputValue === ''
,節省了渲染?
只要檢查'!isEmpty(this.state.searchValue)'是否也工作/我應該做這些檢查? – kayla