2017-06-17 28 views
0

我試圖使用youtubes API與you-tube-api搜索庫,但我有一些麻煩得到新的搜索發生。反應調用Youtube上輸入的退出

我創建了一個名爲search_youtube的新函數,我想在用戶退出輸入時調用它。我現在設置的方式,當我加載html頁面時,函數被連續調用。

什麼是解決這個問題的恰當方法,以便在用戶退出輸入時呈現新的搜索。

class SearchBar extends Component { 
    constructor(props) { 
    super(props) 

    this.state = { 
      videos: [], 
      selectedVideo: null 
     } 
    } 

    searchYoutube(event) { 
    console.log("called") 
    YTSearch({ key: API_KEY, term: event.target.value }, (videos) => { 
     this.setState({ 
     videos: videos, 
     selectedVideo: videos[0] 
     }) 
    }); 
    } 

    render() { 
    return(
     <div className="search-bar"> 
     <input onBlur={this.search_youtube(event)} /> 
     </div> 
    ) 
    } 
} 
+0

根據[react-docs](https://facebook.github.io/react/docs/events.html#focus-events)''''''onBlur'''應該是你之後...這個顯然不工作? – archae0pteryx

回答

0

他們正在裝載每次你加載你的頁面,因爲它是你的渲染功能裏面,您所調用的函數,而不是傳遞函數的onblur,該行改成這樣:

<input onBlur={event => this.search_youtube(event)} /> 

觀測值。如果你想了解更多關於箭頭功能請查看article