2016-12-13 105 views
1

我創建了一個簡單的應用程序使用YouTube的API來搜索視頻,但是當我使用npm start它並沒有給我任何錯誤,但給我的警告Warning: Unknown prop onItemSearched on <searchItem> tag. Remove this prop from the element. in searchItem (created by listItem) in div (created by listItem) in listItemReactJS - 警告:未知道具

這裏是我的代碼:

var React = require('react'); 
var Item = require('./item.jsx'); 
var searchItem = React.createClass({ 
    getInitialState : function() { 
     return { 
      'queryString' : '' 
     }; 
    }, 
    handleSearchClicked : function() { 
     this.props.onItemSearched(this.state); 

     this.setState({ 
      'queryString' : '' 
     }); 
    }, 

    handleChangedNameItem : function(e) { 
     e.preventDefault(); 
     this.setState({ 
      'queryString' : e.target.value 
     }); 
    }, 

    render : function() { 
     return (
      <div> 
       <label> 
        <input id="query" type="text" onChange={this.handleChangedNameItem} value={this.state.queryString} placeholder="Search videos..." /> 
        <button id="search-button" onClick={this.handleSearchClicked}>Search</button> 
       </label> 
      </div> 
     ); 
    } 
}); 

這是什麼樣的listItem我展示我的結果

var listItem = React.createClass({ 
    getInitialState : function() { 
     return { 
      'results' : [] 
     }; 
    }, 

    handleQuerySearch : function(query) { 
     var req = gapi.client.youtube.search.list({ 
      'part': 'snippet', 
      'type': 'video', 
      'q' : encodeURIComponent(query).replace(/%20/g, "+"), 
      'order' : 'viewCount', 
     }); 

     //execute request 
     req.execute((res) => { 
      var results = res.result; 
      this.setState({ 
       'results' : results.items 
      }); 
     }); 
    }, 

    render : function() { 
     var listItem = this.state.results.map(item => { 
      return(
       <Item title={item.snippet.title} videoid={item.id.videoId} /> 
      ); 
     }); 

     return (
      <div> 
       <searchItem onItemSearched={this.handleQuerySearch} /> 
       <div className="list-item"> 
        {listItem} 
       </div> 
      </div> 
     ); 
    } 
}); 

module.exports = listItem; 
+0

,可能是警告是怎樣被接收的道具?如果不知道問題是什麼,不能做太多;) –

+0

我編輯:) –

回答

1

陣營希望所有組件以課堂形式編寫。這意味着名稱需要大寫。

searchItem必須SearchItem

您也可以定義將在搜索項

var SearchItem = React.createClass({ 
    propTypes: { 
     onItemSearched: React.PropTypes.func 
    }, 
    .... 
}); 
+0

你能解釋更具體的,我是一個新手與React,太過分了,我已經改變searchItem到SearchItem,但它不工作: ( –

+0

ya !!它的工作,太多了,ü拯救了我的一天<3 –

+1

@ M.Tae真棒!名稱更改(大寫)是需要發生的事情,組件上指定的propType不是必需的,但它非常好並幫助定義組件。我建議你繼續使用它:) –

相關問題