1
1
import React , { Component } from 'react' ;
2
- import { inject , observer } from 'mobx-react' ;
2
+ import { observable , action } from 'mobx' ;
3
+ import { observer , inject } from 'mobx-react' ;
3
4
import Button from './Button' ;
4
5
import { fetchStories } from '../api/story' ;
5
6
6
- const applyQueryState = query => ( ) => ( {
7
- query
8
- } ) ;
9
-
10
7
@inject ( 'storyStore' ) @observer
11
8
class SearchStories extends Component {
9
+ @observable query = '' ;
10
+
12
11
constructor ( props ) {
13
12
super ( props ) ;
14
13
15
- this . state = {
16
- query : '' ,
17
- } ;
18
-
19
14
this . onChange = this . onChange . bind ( this ) ;
20
15
this . onSubmit = this . onSubmit . bind ( this ) ;
21
16
}
22
17
18
+ @action
23
19
onSubmit ( event ) {
24
20
const { storyStore } = this . props ;
25
- const { query } = this . state ;
26
21
27
- if ( query ) {
28
- fetchStories ( query )
22
+ if ( this . query ) {
23
+ fetchStories ( this . query )
29
24
. then ( result => storyStore . setStories ( result . hits ) )
30
25
. catch ( error => storyStore . setError ( error ) ) ;
31
26
32
- this . setState ( applyQueryState ( '' ) ) ;
27
+ this . query = '' ;
33
28
}
34
29
35
30
event . preventDefault ( ) ;
36
31
}
37
32
33
+ @action
38
34
onChange ( event ) {
39
35
const { value } = event . target ;
40
- this . setState ( applyQueryState ( value ) ) ;
36
+ this . query = value ; ;
41
37
}
42
38
43
39
render ( ) {
44
40
return (
45
41
< form onSubmit = { this . onSubmit } >
46
42
< input
47
43
type = "text"
48
- value = { this . state . query }
44
+ value = { this . query }
49
45
onChange = { this . onChange }
50
46
/>
51
47
< Button type = "submit" >
@@ -56,4 +52,4 @@ class SearchStories extends Component {
56
52
}
57
53
}
58
54
59
- export default SearchStories ;
55
+ export default SearchStories ;
0 commit comments