[英]Passing Props in React Functional Component
I am trying to understand how props work.我试图了解道具是如何工作的。 At the moment I have an array which I want to send to another component.
目前我有一个要发送到另一个组件的数组。 But the input components shows the props value as
但是输入组件将 props 值显示为
"Undefined"
“不明确的”
. . Below is my code:
下面是我的代码:
import React from 'react;
import ReceiveData from "./ReceiveData";
const Data =() = {
const sendData = ["1A", "2A", "3A", "4A", "1B", "2B", "3B", "1C", "2C", "3C", "4C", "1D", "2D", "3D", "4D", "4B"];
<ReceiveData data ={sendData} />
}
export default Data;
Code for ReceiveData component is as follows: ReceiveData 组件的代码如下:
import React from 'react;
function ReceiveData(props){
console.log("Props Output: ", props.data);
}
export default ReceiveData;
Where am I going wrong?我哪里错了? Sorry of my question is very trivial.
对不起,我的问题很琐碎。 Thanks in advance,
提前致谢,
There are few syntax issues like single quote missing on both files, and =>
arrow was missing in Data()
function and also return
statment missing几乎没有语法问题,例如两个文件上都缺少单引号,并且
Data()
function 中缺少=>
箭头,并且还缺少return
语句
I write updated code我写了更新的代码
import React from 'react';
import ReceiveData from "./ReceiveData";
const Data = () => {
const sendData = ["1A", "2A", "3A", "4A", "1B", "2B", "3B", "1C", "2C", "3C", "4C", "1D", "2D", "3D", "4D", "4B"];
return <ReceiveData data ={sendData} />
}
export default Data;
For the second file对于第二个文件
import React from 'react';
function ReceiveData(props){
console.log("Props Output: ", props.data);
return <div>html code here</div>
}
export default ReceiveData;
const Data = () => {
const sendData = ["1A", "2A", "3A", "4A", "1B", "2B", "3B", "1C", "2C", "3C", "4C", "1D", "2D", "3D", "4D", "4B"];
return <ReceiveData data={sendData} />;
};
You missed a couple of things in your Data function component.您在 Data function 组件中遗漏了几件事。 A return statement and missing arrow on your fat arrow syntax.
粗箭头语法上的返回语句和缺少箭头。
Please, try to below code.请尝试以下代码。
const Data = () => {
const sendData = ["1A", "2A", "3A", "4A", "1B", "2B", "3B", "1C", "2C", "3C", "4C", "1D", "2D", "3D", "4D", "4B"];
return (<ReceiveData data ={sendData} />)
}
function ReceiveData(props){
console.log("Props Output: ", props.data);
return <div>something</div>
}
First, Data component return ReceiveData component.首先,Data 组件返回 ReceiveData 组件。 And modify arrow function.
并修改箭头function。
Second, Add something return element in ReceiveData component.其次,在 ReceiveData 组件中添加一些返回元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.