Reputation: 187
I am new to react js. How to map through the data I am getting json response from ajax.I know i did wrong some place But i don't know where.This the error I am getting
Uncaught TypeError: this.state.data.map is not a function
/**
* 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 ;
Upvotes: 2
Views: 641
Reputation: 281884
Your state data is not defined as an array. Correct it as:
getInitialState:function () {
return{
default:10,
data: [],
color:'#58FA58'
}
}
Upvotes: 1
Reputation: 6803
Add
getInitialState: function() {
return{
default:10,
data:[],
color:'#58FA58'
}
}
Upvotes: 1
Reputation: 10421
set data:[ ] in
getInitialState:function () {
return{
default:10,
data:[],
color:'#58FA58'
}
},
and check
componentDidMount:function () {
api.getemail(this.state.default).then(function (response) {
console.log('CHECK',response) // <----------------
this.setState({
data:response
})
}.bind(this))
},
Upvotes: 3