如何在React中动态填充选择选项的setState?TypeError:this.state.schoolName.map不是函数

手部HM
class Schoolextends React.Component {

   constructor(){
      super();
      this.state = { 
         schoolName:[] 
      }
    }

    getMySchools(){
      let arraySchools=[];
      const db=fire.firestore();
      db.collection("schoolNames").get().then((snapshot)=>{
        snapshot.forEach((doc)=>{
        // console.log(doc.data())
          // return(<option>{doc.data()}</option>)
        arraySchools.push(doc.data())
        })
        this.setState({schoolName:arraySchools})
      })
    }

    componentDidMount(){
      this.getMySchools();
    }
    
    render(){
       return (
           <Form.Group controlId="exampleForm.ControlSelect1">
             <Form.Label>Select Schools</Form.Label>
             <Form.Control as="select" onChange={(e) => this.setState({schoolName: e.target.value})}>
                    <option></option>
            {this.state.schoolName.map((index)=>{
              return(<option value={index.addSchool}>{index.addSchool}</option>)
            })}
            </Form.Control>
       )
    }
 }

在上面的代码中,我从中获取学校的名称,firestore并将其填充在选择下拉列表中。并且在选择任何选项时,它会将值存储在schoolName对象数组中作为setState,但会抛出错误TypeError: this.state.schoolName.map is not a function

红男爵

这是因为您在此处将其设置为字符串:

onChange={(e) => this.setState({schoolName: e.target.value})}

然后尝试映射不可能的字符串

您需要this.state.schoolName使用一组对象更新状态,然后在其上进行映射。

或根本不映射它,而只是将其引用为渲染中的对象

只是将其更新为对象

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

TypeError:state.confirmed.map不是函数

来自分类Dev

TypeError:this.state.employees.map不是函数

来自分类Dev

TypeError:this.state.tasks.map不是函数

来自分类Dev

TypeError:this.state.userDtl.map不是函数

来自分类Dev

如何修复 this.state.*.map 不是函数?

来自分类Dev

React JS-TypeError:this.state.data.map不是函数

来自分类Dev

反应| TypeError:this.state.cars.map不是函数

来自分类Dev

未处理的拒绝(TypeError):this.state.personals.map不是函数

来自分类Dev

TypeError:undefined不是函数(在'... this.state.profile.map ...'附近)

来自分类Dev

ReactJS TypeError:_this3.state.Objects.map 不是函数

来自分类Dev

React Native this.state.questions.map() 不是函数

来自分类Dev

类型错误:this.state.article.map 不是函数 REACT

来自分类Dev

Laravel + React:this.state.tasks.map 不是函数

来自分类Dev

类型错误:this.state.user.map 不是反应中的函数

来自分类Dev

TypeError:this.state.rates.map不是一个函数-React.js

来自分类Dev

.Net核心-React-TypeError:this.state.values.map不是函数-App.UseEndpoint

来自分类Dev

反应TypeError:this.state.List.map不是一个函数

来自分类Dev

反应错误:未处理的拒绝(TypeError):this.state.categories.map不是函数

来自分类Dev

this.state.events.map不是函数

来自分类Dev

this.state.post.map不是函数

来自分类Dev

TypeError:this.state.show不是函数

来自分类Dev

map不是reactJS中的函数

来自分类Dev

map 不是列表中的函数

来自分类Dev

收到错误this.state.datas.map不是函数

来自分类Dev

未定义不是函数(在'... this.state.results.map ...'附近)

来自分类Dev

类型错误:this.state.patients.map 不是函数

来自分类Dev

类型错误:this.state.champions.map 不是函数

来自分类Dev

类型错误:this.state.userInfo.map 不是函数

来自分类Dev

反应类型错误:this.state.entries.map 不是函数

Related 相关文章

  1. 1

    TypeError:state.confirmed.map不是函数

  2. 2

    TypeError:this.state.employees.map不是函数

  3. 3

    TypeError:this.state.tasks.map不是函数

  4. 4

    TypeError:this.state.userDtl.map不是函数

  5. 5

    如何修复 this.state.*.map 不是函数?

  6. 6

    React JS-TypeError:this.state.data.map不是函数

  7. 7

    反应| TypeError:this.state.cars.map不是函数

  8. 8

    未处理的拒绝(TypeError):this.state.personals.map不是函数

  9. 9

    TypeError:undefined不是函数(在'... this.state.profile.map ...'附近)

  10. 10

    ReactJS TypeError:_this3.state.Objects.map 不是函数

  11. 11

    React Native this.state.questions.map() 不是函数

  12. 12

    类型错误:this.state.article.map 不是函数 REACT

  13. 13

    Laravel + React:this.state.tasks.map 不是函数

  14. 14

    类型错误:this.state.user.map 不是反应中的函数

  15. 15

    TypeError:this.state.rates.map不是一个函数-React.js

  16. 16

    .Net核心-React-TypeError:this.state.values.map不是函数-App.UseEndpoint

  17. 17

    反应TypeError:this.state.List.map不是一个函数

  18. 18

    反应错误:未处理的拒绝(TypeError):this.state.categories.map不是函数

  19. 19

    this.state.events.map不是函数

  20. 20

    this.state.post.map不是函数

  21. 21

    TypeError:this.state.show不是函数

  22. 22

    map不是reactJS中的函数

  23. 23

    map 不是列表中的函数

  24. 24

    收到错误this.state.datas.map不是函数

  25. 25

    未定义不是函数(在'... this.state.results.map ...'附近)

  26. 26

    类型错误:this.state.patients.map 不是函数

  27. 27

    类型错误:this.state.champions.map 不是函数

  28. 28

    类型错误:this.state.userInfo.map 不是函数

  29. 29

    反应类型错误:this.state.entries.map 不是函数

热门标签

归档