我是React中的新成員,但我構建了一個簡單的應用程序來搜索GitHub用戶只需輸入用戶名。它必須給我關於用戶和他們各自的資源庫的信息,但我有一個問題白衣代碼:錯誤:未被捕獲(承諾)TypeError:this.props.repos.map不是函數
錯誤:
Uncaught (in promise) TypeError: this.props.repos.map is not a function
Uncaught (in promise) TypeError: Cannot read property '_currentElement' of null
代碼:
var React = require('react');
var UserRepos = React.createClass({
getInitialState: function() {
return {
reposCount: 0,
}
},
componentWillReceiveProps: function (props) {
console.log(props);
this.setState({reposCount: props.repos.length});
},
render: function() {
var repos = this.props.repos.map(function (repo, key) {
return (
<div key={key} className="thumbnail">
<div className="caption">
<h3>{repo.name}
<span className="badge">{repo.stargazers_count} STARS</span>
</h3>
<p>{repo.description}</p>
<p>
<a href={repo.html_url} className="btn btn-primary" role="button">Repository</a>
<a href={repo.html_url + '/issues'} className="btn btn-default" role="button">Issues ({repo.open_issues}) </a>
</p>
</div>
</div>
);
});
return (
<div>
<h2>{this.state.reposCount} repositories</h2>
{repos}
</div>
)
}
});
module.exports =UserRepos;
這意味着無論你用什麼來初始化'props.repos'都不是一個數組。 – Pointy
你在渲染函數中爲this.prop.repos獲取了什麼值 –
在你的props.repos被設置之前你的渲染被調用。在渲染中,通過檢查props.repos是否存在來解決這個問題;如果不是,則將其設置爲空數組。當props.repos確實被創建時,渲染將被再次調用,然後會正常工作。 –