import React from 'react';
import ReactDOM from 'react-dom';
var axios = require('axios');
class Application extends React.Component {
constructor() {
super();
this.handleClick = this.handleClick.bind(this);
this.state = {
dropdownItems: []
};
}
deleteDd(index) {
let dropdownItems = this.state.dropdownItems.filter(item => item!==index);
this.setState({dropdownItems: dropdownItems});
}
handleClick() {
let dropdownItems = [...this.state.dropdownItems];
dropdownItems.push(dropdownItems.length);
this.setState({dropdownItems: dropdownItems});
}
getInitialState() {
return {
company: []
}
}
//trying to get json data into dropdown by passing the json object into the url
componentDidMount(){var _this = this;
this.serverRequest = axios
.get("myurl")
.then(function(result) {
_this.setState({
company: result.data.company
});
//console.log(jobs);
})
}
componentWillUnmount(){
this.serverRequest.abort();
}
render() {
let dropdowns = this.state.dropdownItems.map(item =>
(<MyDropdown key = {item} num = {item} onDeleteMe ={this.deleteDd.bind(this, item)} />));
return (
<div>
<h1 className="text-left page-title">Higher</h1>
<h2 className="text-left">CTR</h2>
<h3 className="text-left">ABC</h3>
<div>
<form>
<select className="dropdown menu dropdown-style" data-dropdown-menu>
<option defaultValue>Choose one</option>
<option value="test">test</option>
<option value="test1">test1</option>
</select>
//here is where all my json data resides in company
<h1>Companies!</h1>
{this.state.company.map(function(company) {
return (
<div key={company.id} className="company">
{company.Company}
</div>);})}
</form>
我收到此錯誤的不確定「地圖」「遺漏的類型錯誤:無法讀取的未定義的屬性‘地圖’」我試圖JSON數據加載到一個下拉列表,請幫助。我嘗試了所有可能的方式,但仍然無法確定問題所在,非常感謝所有幫助。遺漏的類型錯誤:無法讀取性能在reactjs
的[無法讀取的未定義的屬性「地圖」]可能的複製(http://stackoverflow.com/questions/24706267/cannot-進行檢查未定義的讀取屬性映射) –
當您嘗試映射時,可能'this.state.company'未定義,因爲它被異步填充。只需檢查'this.state.company'是否未定義。試試'this.state.company && this.state.company.map .....' –