最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - When I pass array in a react functional component, it turns to an object - Stack Overflow

programmeradmin3浏览0评论

Hi I have to pass array as props to a functional ponent.

import React from "react";
import { render } from "react-dom";

const App = () => {
  const FBS = ({ figures }) => {
    console.log(typeof figures);
    return figures.map((item, key) => <p key={key}>{item.description}</p>);
  };
  const figures = [
    {
      config: 112,
      description: "description text 1"
    },
    {
      config: 787,
      description: "description text 2"
    }
  ];

  return (
    <div>
      {/* <FBS {...figures} /> */}
      <FBS figures={figures} />
    </div>
  );
};

render(<App />, document.getElementById("root"));
<script src=".6.3/umd/react.production.min.js"></script>
<script src=".6.3/umd/react-dom.production.min.js"></script>
<body>
<div id='root' />
</body>

Hi I have to pass array as props to a functional ponent.

import React from "react";
import { render } from "react-dom";

const App = () => {
  const FBS = ({ figures }) => {
    console.log(typeof figures);
    return figures.map((item, key) => <p key={key}>{item.description}</p>);
  };
  const figures = [
    {
      config: 112,
      description: "description text 1"
    },
    {
      config: 787,
      description: "description text 2"
    }
  ];

  return (
    <div>
      {/* <FBS {...figures} /> */}
      <FBS figures={figures} />
    </div>
  );
};

render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare./ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<body>
<div id='root' />
</body>

But it is converted to an object in the child ponent. Please look at the render function. When I pass array as {...figures} I don't get it as Array in the FBS ponent due to which I can't run map function on it. Whereas when I pass it as figures={figures}, I get an array. I want to pass it as {...figures}.

Please help

Please look at my code for better understanding. here

Share Improve this question asked Mar 22, 2019 at 14:03 RickieRickie 6513 gold badges11 silver badges21 bronze badges 4
  • 1 Maybe { [...figures] }? – Pointy Commented Mar 22, 2019 at 14:04
  • The codesandbox works perfectly for me. – Vladimir Bogomolov Commented Mar 22, 2019 at 14:06
  • As Vladimir said, the codesandbox work just fine.. Moreover, if you write console.log(Array.isArray(figures)); it returns true – Jolly Commented Mar 22, 2019 at 14:07
  • 1 It works alright, but what OP is wanting is imo, the same behavior when figures={figures} is changed to just {...figures} – Amit Joki Commented Mar 22, 2019 at 14:09
Add a ment  | 

2 Answers 2

Reset to default 3

You need to have additional object which will have pair of key and value which will be destructured as your props to the React Component.

const props = {
  figures, // shorter way of writing figures: figures
  // Any other objects you'd like to pass on as props
}

and then, you can do:

<FPS {...props} />

Updated Code

Basically you can only destructure an object in the React Component because then the destructured object's key-value pairs will bee props to the ponent.

For better understanding,

const arr = [{ a: 'a'}]
{...arr}

will give:

{
  0: {a: 'a'}
}

because 0 is the key as it is an array as opposed to an object, so what you were really doing was passing a prop with name 0 instead of figures and figures was undefined and hence the error.

You can use something like this:

import React from "react";
import Figure from './Figure';
import { render } from "react-dom";

const App = () => {
  const figures = [
    {
      config: 112,
      description: "description text 1"
    },
    {
      config: 787,
      description: "description text 2"
    }
  ];

  return (
    <div>

      { 
        figures.map((figure, key) => {
          return <Figure key={key} {...figure}/>
        })
      }

    </div>
  );
};

render(<App />, document.getElementById("root"));

And create a ponent called Figure like this:

import React from "react";

const Figure = (props) => {
   return (
    <div>
     <p>{props.description}</p>
     <p>{props.config}</p>
    </div>
  );
};

export default Figure;

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论