简体   繁体   English

扩展运算符将所有其他道具传递给组件。 反应.js

[英]Spread operator to pass all other props to a component. React.js

I'm having trouble understanding the spread operator when I want to pass all other props to a component.当我想将所有其他道具传递给组件时,我无法理解传播运算符。

Any help would be appreciated.任何帮助,将不胜感激。

import React, { Fragment } from "react";
import SiteCard from "./SiteCard";

const SiteList = ({ sites }) => {
  return (
    <Fragment>
      {sites.map((site) => {
        return (
          <SiteCard
            key={site.login.uuid}
            image={site.picture.large}
            firstName={site.name.first}
            lastName={site.name.last}
            city={site.location.city}
            country={site.location.country}
            sensors={site.dob.age}
            otherSiteProps={...site} // how can I pass the site props here?
          />
        );
      })}
    </Fragment>
  );
};

export default SiteList;

You are almost there with the solution.您几乎可以找到解决方案。

You need to pass it as otherSiteProps={{...site}} .您需要将其作为otherSiteProps={{...site}}传递。 This is if you want to pass site as an object to otherSiteProps property of SiteCard .这是如果您想将site作为 object 传递给SiteCardotherSiteProps属性。

If you want to spread site and have multiple props for component SiteCard you do it like this:如果您想传播site并为组件SiteCard拥有多个道具,您可以这样做:

<SiteCard
  key={site.login.uuid}
  image={site.picture.large}
  firstName={site.name.first}
  lastName={site.name.last}
  city={site.location.city}
  country={site.location.country}
  sensors={site.dob.age}
  {...sites}
/>

This in case that sites is an object.如果sites是 object。 If site is an array, this wont work.如果site是一个数组,这将不起作用。

You just need to write:你只需要写:

<SiteCard
  key={site.login.uuid}
  image={site.picture.large}
  firstName={site.name.first}
  lastName={site.name.last}
  city={site.location.city}
  country={site.location.country}
  sensors={site.dob.age}
  {...site} // how can I pass the site props here?
/>

But wait, why you're making so complicated?但是等等,你为什么要搞得这么复杂? You can just use:您可以使用:

<SiteCard {...site} />

Now, in your SiteCard component use required props.现在,在您的 SiteCard 组件中使用所需的道具。

And if I were you, I would not have separated SiteCard component for this scenario.如果我是你,我不会在这种情况下分离SiteCard组件。 I would just write:我只想写:

{sites.map((site) => {
  return (
    // everything here I will utilize in html.
  );
})}

暂无
暂无

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

相关问题 在 React.js 中将 props 传递给父组件 - Pass props to parent component in React.js React.js 发送 id 从<link to>或将道具传递给其他组件 - React.js send id from <Link to> or pass props to other component 当父组件向子组件传递数据并渲染子组件时显示加载栏。 反应.JS - show loading bar when parent component pass data to and render child component. React.JS 如何将对象作为道具传递给 React.js 中的子功能组件? - How to pass an object as props to a child Functional Component in React.js? React Native:当在子组件上使用spread运算符时,将样式作为props传递的正确方法是什么 - React Native: what is a proper way to pass style as props, when using spread operator on the child component React.js - 子组件道具未定义 - React.js - child component props undefined React.js:如何将道具传递给组件? - React.js: How to pass props to components? 如何使用 react.js 中的编程路由将道具从子组件(addform)传递到主应用程序组件 - How to pass props from child component (addform) to main app component using programmatic routing in react.js 如何在 React.js 中使用 onClick() 事件将道具从一个组件传递到另一个组件 - How to pass props from one Component to another component with onClick() event in React.js 如何通过道具[REACT.JS]将图片网址从父组件传递到子组件 - How can I pass image url from parent component to child component via props [REACT.JS]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM