Skip to content

Commit ace7de6

Browse files
authored
Merge pull request #185 from react-redux-mvvm/master
remove findDOMNode from searchbar
2 parents bf08a70 + e43fcb2 commit ace7de6

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/components/searchbar/searchbar.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ class SearchBar extends React.Component {
8484

8585
this.setState({text: '', clearing: true});
8686
if(this.props.onClear) this.props.onClear(e);
87-
ReactDOM.findDOMNode(this.refs.searchInput).focus()
87+
// In most cases, you can attach a ref to the DOM node and avoid using findDOMNode at all.
88+
// When render returns null or false, findDOMNode returns null.
89+
// 这里是截取官网的说明,在ref回调函数内确实会返回null,尤其是配合redux使用的时候,这个时候需要对其进行null判断
90+
this.refs.searchInput.focus();
91+
// ReactDOM.findDOMNode(this.refs.searchInput).focus()
8892
if(this.props.onChange) this.props.onChange('',e);
8993
}
9094

@@ -134,7 +138,12 @@ class SearchBar extends React.Component {
134138
</div>
135139
<label
136140
className='weui-search-bar__label'
137-
onClick={e=>ReactDOM.findDOMNode(this.refs.searchInput).focus()}
141+
onClick={()=>{
142+
let searchInput = this.refs.searchInput;
143+
if (searchInput) {
144+
searchInput.focus();
145+
}
146+
}}
138147
style={{display: this.state.text ? 'none': null}}
139148
>
140149
<Icon value='search'/>

0 commit comments

Comments
 (0)