简体   繁体   English

React js 将道具传递给组件

[英]React js Passing props to a component

Hello basically I need to pass some data to a component to be able to popular that component:您好,基本上我需要将一些数据传递给组件才能流行该组件:

const Data = {
  name: "a",
  title: "b"
};
export default function App() {
  return (
    <Div className="App">
      <Card name={Data.name} title={Data.title} />
    </Div>
  );
}

my card componen The component I need to receive data:我的卡组件我需要接收数据的组件:

const Card = ({ props }) => {
  return (
    <div>
      <h1>{props.name}</h1>
      <p>{props.title}</p>
    </div>
  );
};

export default Card;

For some reason I am not getting these props on my card component出于某种原因,我的卡片组件上没有这些道具

could someone help me with a solution to move props from a functional component to a functional component有人可以帮我解决将道具从功能组件移动到功能组件的解决方案吗

example:例子:

https://codesandbox.io/s/friendly-swanson-syuln https://codesandbox.io/s/friendly-swanson-syuln

Your component declaration is wrong.您的组件声明是错误的。 You should only use brackets to destructure props, not for the entire props object.你应该只使用括号来解构 props,而不是整个 props 对象。

const Card = (props) => {

just get rid of the curly braces around props in cards.js去掉cards.js中props周围的花括号

import React from "react";

const Card = ( props ) => {
  return (
    <div>
      <h1>{props.name}</h1>
      <p>{props.title}</p>
    </div>
  );
};

export default Card;

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

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