简体   繁体   English

ReactJS抛出“ map”未定义错误

[英]ReactJS throws “map” is not defined Error

I am not very familiar with JavaScript, but have to implement a table in ReactJS for my current project. 我对JavaScript不太熟悉,但是必须为当前项目在ReactJS中实现一个表。 At the part bellow I get an error: map is not defined . 在下面,我得到一个error: map is not defined I have done some Error-Research but could not find an satisfying answer here or on Google. 我做了一些错误研究,但在这里或在Google上找不到满意的答案。

   render() {
        const { hits } = this.props
        return (
            <div style={{width: '100%', boxSizing: 'border-box', padding: 8}}>
                <table className="sk-table sk-table-striped" style={{width: '100%', boxSizing: 'border-box'}}>
                    <thead>
                        <tr>
                            <th>Title</th>
                            <th>Author</th>
                            <th>Keywords</th>
                        </tr>
                    </thead>
                    <tbody>
            {map(hits, hit => (
              <tr key={hit._id}>
                <td>{hit._source.title}</td>
                <td>{hit._source.year}</td>
                <td>{hit._source.imdbRating}</td>
              </tr>
            ))}
          </tbody>
                </table>
            </div>
        )
    }

Could someone point me in the right direction? 有人可以指出我正确的方向吗?

You have to use the map function on the hits const you create at the top. 您必须在顶部创建的匹配常数上使用map函数。 Like this: 像这样:

{hits.map(hit => (
  <tr key={hit._id}>
    <td>{hit._source.title}</td>
    <td>{hit._source.year}</td>
    <td>{hit._source.imdbRating}</td>
  </tr>
))}

map is a function defined on the Array prototype in javascript. map是在javascript的Array原型上定义的函数。 You are calling map here on the global object, which does not have that method defined. 您正在此处未定义该方法的全局对象上调用map。 Thus, you get an undefined error. 因此,您将得到一个未定义的错误。

hits.map is probably what you are looking for. hits.map可能正是您想要的。

Is your example from http://docs.searchkit.co/v2.0.0/components/basics/hits.html 是您来自http://docs.searchkit.co/v2.0.0/components/basics/hits.html的示例

In this case You need declare map ... Put : 在这种情况下,您需要声明map ... Put:

import { map } from 'lodash'

at begining of your file , after import {....} from 'searchkit' 从'searchkit'导入{....}后,在文件开头

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM