Javascript 无法设置状态REACT/JS:无法读取属性';地图';未定义的
您好,我正在尝试循环一个列表对象,并使用列表的每个条目呈现一个对象。然而,我一直遇到同样的错误。。我还想知道如何才能看到我是否正确设置了状态 这是我得到的错误Javascript 无法设置状态REACT/JS:无法读取属性';地图';未定义的,javascript,reactjs,rest,Javascript,Reactjs,Rest,您好,我正在尝试循环一个列表对象,并使用列表的每个条目呈现一个对象。然而,我一直遇到同样的错误。。我还想知道如何才能看到我是否正确设置了状态 这是我得到的错误 Uncaught TypeError: Cannot read property 'map' of undefined at Feed.render (bundle.js:11417) at finishClassComponent (bundle.js:7881) at updateClassComponent
Uncaught TypeError: Cannot read property 'map' of undefined
at Feed.render (bundle.js:11417)
at finishClassComponent (bundle.js:7881)
at updateClassComponent (bundle.js:7878)
at beginWork (bundle.js:7974)
at performUnitOfWork (bundle.js:8294)
at workLoop (bundle.js:8318)
at HTMLUnknownElement.callCallback (bundle.js:6296)
at Object.invokeGuardedCallbackDev (bundle.js:6312)
at invokeGuardedCallback (bundle.js:6251)
at performWork (bundle.js:8354)
以下是我在API上的json:
{
"next": "",
"posts": [
{
"postid": 3,
"url": "/api/v1/p/3/"
},
{
"postid": 2,
"url": "/api/v1/p/2/"
},
{
"postid": 1,
"url": "/api/v1/p/1/"
}
],
"url": "/api/v1/p/"
}
这是我的obj课程:
constructor(props) {
// Initialize mutable state
super(props);
this.state = { posts: []};
}
componentDidMount() {
// Call REST API to get number of likes
fetch(this.props.url, { credentials: 'same-origin' })
.then((response) => {
if (!response.ok) throw Error(response.statusText);
return response.json();
})
.then((data) => {
this.setState({
posts: data.posts,
});
})
.catch(error => console.log(error));
}
render() {
const nums = [1,2,3,4,5,6]
var newData = Array.from(this.state.posts)
return (
<ul>
{this.state.newData.map((post)=> (
<li>post</li>
))}
</ul>
);
}
}
构造函数(道具){
//初始化可变状态
超级(道具);
this.state={posts:[]};
}
componentDidMount(){
//调用RESTAPI以获取喜欢的数量
获取(this.props.url,{credentials:'same origin'})
。然后((响应)=>{
如果(!response.ok)抛出错误(response.statusText);
返回response.json();
})
。然后((数据)=>{
这是我的国家({
posts:data.posts,
});
})
.catch(错误=>console.log(错误));
}
render(){
常量nums=[1,2,3,4,5,6]
var newData=Array.from(this.state.posts)
返回(
{this.state.newData.map((post)=>(
- 职位
))}
);
}
}
组件的状态对象中没有存储新数据
newData
的作用域是本地的render()
方法
这应该通过简单地将This.state.newData.map(…)
替换为newData.map(…)
This.state.newData.map
来解决,你的意思是newData.map
?