猿问

TypeError : ...map 不是函数 - react.js

在我的react.js应用程序中,我有一个这样的功能:


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


开满天机
浏览 131回答 1
1回答

ibeautiful

根据评论和 stackblitz 链接,问题是您没有从函数内部返回值import React, { Component } from 'react';import { render } from 'react-dom';import Hello from './Hello';import './style.css';class ClassRoomUnit extends Component{&nbsp; &nbsp; displayName;&nbsp; &nbsp; classID;&nbsp; &nbsp; render() {&nbsp; &nbsp; &nbsp; &nbsp; return(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {this.props.displayName}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {this.props.classID}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; }}class App extends Component {&nbsp; state={&nbsp; &nbsp; c:[1,2,3]&nbsp; }&nbsp; classRoom(classRooms)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; console.log(classRooms);&nbsp; &nbsp; &nbsp; &nbsp; if(classRooms!==null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const updatedClass = classRooms.map((classRoom)=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return <ClassRoomUnit displayName={"as"} classID={"asdsa"} />;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return updatedClass;&nbsp; &nbsp; }&nbsp; render() {&nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; dynamic list start&nbsp; &nbsp; &nbsp; &nbsp; {this.classRoom(this.state.c)}&nbsp; &nbsp; &nbsp; &nbsp; dynamic list end&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; );&nbsp; }}render(<App />, document.getElementById('root'));
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答