File tree 1 file changed +11
-2
lines changed
1 file changed +11
-2
lines changed Original file line number Diff line number Diff line change @@ -84,7 +84,11 @@ class SearchBar extends React.Component {
84
84
85
85
this . setState ( { text : '' , clearing : true } ) ;
86
86
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()
88
92
if ( this . props . onChange ) this . props . onChange ( '' , e ) ;
89
93
}
90
94
@@ -134,7 +138,12 @@ class SearchBar extends React.Component {
134
138
</ div >
135
139
< label
136
140
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
+ } }
138
147
style = { { display : this . state . text ? 'none' : null } }
139
148
>
140
149
< Icon value = 'search' />
You can’t perform that action at this time.
0 commit comments