第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

反應 this.state.addroom.map 不是一個函數(shù)

反應 this.state.addroom.map 不是一個函數(shù)

翻閱古今 2022-07-21 21:33:59
所以我試圖用一個按鈕的按鈕 onClick 生成一個 div,但我收到一個錯誤,阻止我這樣做。錯誤:TypeError: this.state.addroom.map is not a function但是我看到,當我單擊按鈕時,它不會顯示錯誤,但也不會生成帶有按鈕的 div。這是我的代碼:import React, { Component } from 'react';import Select, { components } from 'react-select';import styles from '../styles/loginsignup.css'import axios from 'axios'import nextId from "react-id-generator";export default class AccomodationInfo extends Component {    constructor() {        super();        this.state = {            accomcate: null,            addroom: ['one'],            isLoading: true,        }      }     handleClick = event => {        const htmlId = nextId()        event.preventDefault()        const addroom = this.state.addroom        this.setState({ addroom: htmlId })        return (               <div>                {this.state.addroom.map(addrooms => (                  <button key= {addroom.id} className={addrooms.modifier}>                    {addrooms.context}                  </button>                ))}           </div>             );    }    render() {         return(           <div>           <button onClick={this.handleClick}>Add</button>           </div>        )    }}}任何人都知道是什么原因造成的以及我們如何解決它?
查看完整描述

1 回答

?
函數(shù)式編程

TA貢獻1807條經(jīng)驗 獲得超9個贊

您的代碼有一些問題。


首先,addroom您的狀態(tài)是構造函數(shù)中的字符串數(shù)組,但是在handleClick您設置它的方法this.setState({ addroom: htmlId })中,它將設置為 astring并且在string類型上map未定義函數(shù),因此出現(xiàn)錯誤。您應該向數(shù)組中添加一個項目,例如this.setState({ addroom: [...this.state.addroom, htmlId] })


其次,你handleClick不應該返回 jsx,如果你想為你的addroom數(shù)組渲染數(shù)據(jù),你應該在render方法中這樣做,并且handleClick你應該只修改addroom狀態(tài)變量。您可以像這樣實現(xiàn):


render() {

  return (

    <div>

      <button onClick={this.handleClick}>Add</button>

      {this.state.addroom.map((addroom) => (

        <button>{addroom}</button>

      ))}

    </div>

    )

}

最后,您的addrom變量只是一個字符串數(shù)組,因此您無法訪問id,modifier和context該數(shù)組中的一個項目。


查看完整回答
反對 回復 2022-07-21
  • 1 回答
  • 0 關注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號