简体   繁体   English

反应:object 抛出“.keys 不是 function 错误”

[英]React: object throws ".keys is not a function Error"

I'm aware that this error may be quite common and have been answered several times before, but I couldn't find a solution.我知道这个错误可能很常见并且之前已经回答了好几次,但我找不到解决方案。

My code always throws this error: ".map is not a function".我的代码总是抛出这个错误:“.map 不是函数”。 I know that this happens because data is not an array.我知道发生这种情况是因为 data 不是数组。 So I tried to solve this with.keys function but this throws the ".keys is not a function" error.所以我尝试用.keys function 解决这个问题,但这会引发“.keys 不是函数”错误。 I'm declaring const data in the parent component and want to use it in the child component.我在父组件中声明 const 数据并希望在子组件中使用它。

I think my error depends on a false use of.keys.我认为我的错误取决于对.keys 的错误使用。 But after much Googling I'm still not one step further.但经过多次谷歌搜索后,我仍然没有更进一步。

This is my Child-Code:这是我的子代码:

import React from "react";
import Card from 'react-bootstrap/Card';
import Col from 'react-bootstrap/Col';
import Row from 'react-bootstrap/Row';
import Container from 'react-bootstrap/Container';
import {Link} from 'react-router-dom'

const PostsRow = (data) => {
     

{return (
    
    <Container>
      <Row>
        {data.keys(data).map((data) => {
          console.log(data + "is mount")
              return (
        <Col className="col-6 col-md-6 col-lg-3 card">
            <Link to={data.url}>
              <Card className="  text-center ">
                <Card.Img variant="top" src={data.imagesrc} />
                <Card.Body>
                  <Card.Title>{data.title}</Card.Title>
                </Card.Body>
              </Card>
            </Link>
          </Col>
        );
      })}
      </Row>
    </Container>
  );
};

export default PostsRow;

This is home.jsx (parent):这是 home.jsx(父级):

import React from "react";
import './route.css';
import PostsRow from "../components/Content/PostsRow";


const Home = () => {
    
const data = {
  
    title: "Ersti",
    imagesrc: "./490.jpg",
    url: "/meineposts" 
};

  return (
    <div> 
      <PostsRow data={data}/>
  </div>
  );
};

export default Home;

This is working fine as long as const data is declared in the PostsRow.jsx, but when I try to declare it in Home.jsx and use props the above error throws.只要在 PostsRow.jsx 中声明 const 数据就可以正常工作,但是当我尝试在 Home.jsx 中声明它并使用 props 时,会抛出上述错误。

As data is an object.由于data是 object。 To get its keys, you should write: Object.keys(data) .要获取其密钥,您应该编写: Object.keys(data)

And, you have a typo in props destructuring: it should be ({data}) .而且,你在 props 解构中有一个错字:它应该是({data})

Your example data is simply an object, not an array, so you don't need to use map or Object.keys here, you can simply write:您的示例数据只是一个 object,而不是一个数组,因此您不需要在这里使用mapObject.keys ,您可以简单地写:

<Col className="col-6 col-md-6 col-lg-3 card">
  <Link to={data.url}>
    <Card className="text-center">
      <Card.Img variant="top" src={data.imagesrc} />
      <Card.Body>
        <Card.Title>{data.title}</Card.Title>
      </Card.Body>
    </Card>
  </Link>
</Col>

PostsRow will be called with props and data is property of it. PostsRow 将使用 props 调用,而data是它的属性。 so you have to use it like所以你必须像使用它一样

const PostsRow = ({data}) => {

And you've to convert your data to array like而且您必须将数据转换为数组,例如

const data = [{
    title: "Ersti",
    imagesrc: "./490.jpg",
    url: "/meineposts" 
}];

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

相关问题 反应| 功能组件抛出错误useState Object(…)不是函数 - React | Functional component throws error useState Object(…) is not a function Function 循环通过 object 键抛出未定义 - Function looping through object keys throws undefined 具有对象参数的React函数抛出意外令牌 - React function with object params throws an unexpected token 是否可以构造一个对象,以便在请求其键时抛出错误? - Is it possible to construct an object so that it throws an error when its keys are requested? React Native 渲染函数抛出错误 - React Native render function throws error 当尝试在自定义 function - React throws an error “Can't assign to property” scrollLeft “on 1: not an object” When trying use ref in custom function 具有功能的AJAX发送对象在功能处引发错误 - AJAX sending object with functions throws error at function 代码执行抛出错误“对象不是函数” - Code execution throws error “object is not a function” React,错误:对象作为 React 子项无效(找到:object 和键 {data}) - React , Error: Objects are not valid as a React child (found: object with keys {data}) React导入的库抛出错误:无法将undefined或null转换为object - React imported library throws error: cannot convert undefined or null to object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM