[英]TypeError : …map is not a function - react.js
在我的react.js应用程序中,我有一个 function ,如下所示:
state={
c:[]
}
classRoom(classRooms)
{
if(classRooms!==null)
{
const updatedClass = classRooms.map((classRoom)=>{
return <ClassRoomUnit displayName={"as"} classID={"asdsa"} />;
//return <li>{classRoom}</li>;
});
}
}
在我的render
方法中,我有:
render() {
return (
<div>
{this.classRoom(this.state.c)}
</div>
);
}
c
定义在 state 中,它是一个数组。
但它给了我错误,说: classRooms.map is not a function
根据评论和 stackblitz 链接,问题是您没有从 function 内部返回值
import React, { Component } from 'react';
import { render } from 'react-dom';
import Hello from './Hello';
import './style.css';
class ClassRoomUnit extends Component{
displayName;
classID;
render() {
return(
<div>
{this.props.displayName}
{this.props.classID}
</div>
);
}
}
class App extends Component {
state={
c:[1,2,3]
}
classRoom(classRooms)
{
console.log(classRooms);
if(classRooms!==null)
{
const updatedClass = classRooms.map((classRoom)=>{
return <ClassRoomUnit displayName={"as"} classID={"asdsa"} />;
});
}
return updatedClass;
}
render() {
return (
<div>
dynamic list start
{this.classRoom(this.state.c)}
dynamic list end
</div>
);
}
}
render(<App />, document.getElementById('root'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.