admin管理员组文章数量:1416651
I'm learning React and now I'm trying to do a get request and list with map but when I run this code they e up with this error "Unhandled Rejection (TypeError): this.state.features.map is not a function". I have already searched this but I do not understand what is going on.
import React, { Component } from 'react';
import './App.css';
class App extends Component {
constructor() {
super();
this.state = {
features: [{
id: 1,
name: 'Test',
count: 1
}]
}
}
ponentWillMount() {
fetch(";)
.then(response => response.json())
.then(json => {
console.log(json);
this.setState({
features: json,
});
});
console.log(this.state.features)
}
render() {
return (
<div className="App">
<ul>
{
this.state.features.map(function(feature){
return (
<li key={feature.id}><button type="button">Upvote</button> ({feature.count}) <span>{feature.name}</span></li>
)
})
}
</ul>
</div>
);
}
}
export default App;
I'm learning React and now I'm trying to do a get request and list with map but when I run this code they e up with this error "Unhandled Rejection (TypeError): this.state.features.map is not a function". I have already searched this but I do not understand what is going on.
import React, { Component } from 'react';
import './App.css';
class App extends Component {
constructor() {
super();
this.state = {
features: [{
id: 1,
name: 'Test',
count: 1
}]
}
}
ponentWillMount() {
fetch("http://demo6085176.mockable.io/features")
.then(response => response.json())
.then(json => {
console.log(json);
this.setState({
features: json,
});
});
console.log(this.state.features)
}
render() {
return (
<div className="App">
<ul>
{
this.state.features.map(function(feature){
return (
<li key={feature.id}><button type="button">Upvote</button> ({feature.count}) <span>{feature.name}</span></li>
)
})
}
</ul>
</div>
);
}
}
export default App;
Share
Improve this question
edited Mar 16, 2022 at 7:09
VLAZ
29.2k9 gold badges63 silver badges84 bronze badges
asked Jun 20, 2017 at 15:45
Salatiel QueirozSalatiel Queiroz
1531 silver badge8 bronze badges
7
- What says console.log(json) ? – Evgeny Sorokin Commented Jun 20, 2017 at 15:46
- 'json' is not defined – Salatiel Queiroz Commented Jun 20, 2017 at 15:47
-
You have to wait until
fetch
finishes... set something like "loading..." and after fetch is done, map it – Andrew Li Commented Jun 20, 2017 at 15:49 -
Your problem is solved through proper debugging. Given that
json
is undefined when youconsole.log
it, then there may be something wrong with the server's output. – E_net4 Commented Jun 20, 2017 at 15:51 - @AndreLi not agree, he doesn't need to wait until fetch. – Evgeny Sorokin Commented Jun 20, 2017 at 15:52
1 Answer
Reset to default 4In your ponentWillMount, just do this:
ponentWillMount() {
fetch("http://demo6085176.mockable.io/features")
.then(response => response.json())
.then(json => {
console.log(json);
this.setState({ features: json.features });
});
}
The response you get from the API is an object which has a key of features
which is an array of objects of the data you want.
本文标签:
版权声明:本文标题:javascript - ReactJS "Unhandled Rejection (TypeError): this.state.features.map is not a function" - Stack Over 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745255865a2650097.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论