我是新来的反应js。如何通过数据映射我从ajax得到json的回应。我知道我在某个地方做错了但是我不知道在哪里。这个我得到的错误
未捕获的TypeError:this.state.data.map不是函数
/**
* Created by arfo on 6/26/2016.
*/
var React =require('react');
var api = require('../utils');
var Bulkmail = React.createClass({
getInitialState:function () {
return{
default:10,
data:'',
color:'#58FA58'
}
},
componentDidMount:function () {
api.getemail(this.state.default).then(function (response) {
this.setState({
data:response
})
}.bind(this))
},
onSubmit:function (e) {
e.preventDefault();
console.log(this.refs.text.value.trim());
},
onChange:function (e) {
e.preventDefault();
//console.log(this.refs.text.value.trim())
var data = this.refs.text.value.trim();
if(isNaN(data)){
this.setState({
color:'#FE2E2E'
})
}else{
this.setState({
color:'#58FA58'
})
}
},
render:function () {
console.log(this.state.data);
var results = this.state.data;
return(
<div className="bodybox">
<div className="box">
<div className="upc">
<p>Generate Bulk Email</p>
<form onSubmit={this.onSubmit}>
<input onChange={this.onChange} type="text" style={{border:'1px solid '+this.state.color}} ref="text" defaultValue={this.state.default} placeholder="Enter Number"/>
<button>Get Data</button>
</form>
<div className="result">
<ul>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
{this.state.data.map(function (data) {
return <li>data.email</li>
})}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
</ul>
</div>
</div>
<div className="tdown">
<p>Json Format</p>
<textarea defaultValue={"json"} />
</div>
</div>
</div>
)
}
});
module.exports = Bulkmail ;
答案 0 :(得分:3)
在
中设置数据:[]getInitialState:function () {
return{
default:10,
data:[],
color:'#58FA58'
}
},
并检查
componentDidMount:function () {
api.getemail(this.state.default).then(function (response) {
console.log('CHECK',response) // <----------------
this.setState({
data:response
})
}.bind(this))
},
答案 1 :(得分:1)
您的状态数据未定义为数组。把它纠正为:
getInitialState:function () {
return{
default:10,
data: [],
color:'#58FA58'
}
}
答案 2 :(得分:1)
Add
getInitialState: function() {
return{
default:10,
data:[],
color:'#58FA58'
}
}