11import React , { Component } from 'react' ;
2- import { inject , observer } from 'mobx-react' ;
2+ import { observable , action } from 'mobx' ;
3+ import { observer , inject } from 'mobx-react' ;
34import Button from './Button' ;
45import { fetchStories } from '../api/story' ;
56
6- const applyQueryState = query => ( ) => ( {
7- query
8- } ) ;
9-
107@inject ( 'storyStore' ) @observer
118class SearchStories extends Component {
9+ @observable query = '' ;
10+
1211 constructor ( props ) {
1312 super ( props ) ;
1413
15- this . state = {
16- query : '' ,
17- } ;
18-
1914 this . onChange = this . onChange . bind ( this ) ;
2015 this . onSubmit = this . onSubmit . bind ( this ) ;
2116 }
2217
18+ @action
2319 onSubmit ( event ) {
2420 const { storyStore } = this . props ;
25- const { query } = this . state ;
2621
27- if ( query ) {
28- fetchStories ( query )
22+ if ( this . query ) {
23+ fetchStories ( this . query )
2924 . then ( result => storyStore . setStories ( result . hits ) )
3025 . catch ( error => storyStore . setError ( error ) ) ;
3126
32- this . setState ( applyQueryState ( '' ) ) ;
27+ this . query = '' ;
3328 }
3429
3530 event . preventDefault ( ) ;
3631 }
3732
33+ @action
3834 onChange ( event ) {
3935 const { value } = event . target ;
40- this . setState ( applyQueryState ( value ) ) ;
36+ this . query = value ; ;
4137 }
4238
4339 render ( ) {
4440 return (
4541 < form onSubmit = { this . onSubmit } >
4642 < input
4743 type = "text"
48- value = { this . state . query }
44+ value = { this . query }
4945 onChange = { this . onChange }
5046 />
5147 < Button type = "submit" >
@@ -56,4 +52,4 @@ class SearchStories extends Component {
5652 }
5753}
5854
59- export default SearchStories ;
55+ export default SearchStories ;
0 commit comments