[英]Cannot Access props inside function of functional component
I am trying to use props inside function of functional props.我正在尝试在功能道具的功能中使用道具。
const Inputs = (props) => {
console.log(props.printFirstArray);
const FirstInputSet = () => {
console.log(props.printFirstArray)
}
}
First console.log
is logging the value of printFirstArray
, but second console.log
inside FirstInputSet()
function is not logging anything.第一个
console.log
正在记录printFirstArray
的值,但是FirstInputSet()
函数中的第二个console.log
没有记录任何内容。 Edit: Minimal Code编辑:最少代码
const Inputs = (props) => {
const FirstInputSet = () => {
return (
<>
<div className="first input-set">
{props.printFirstArray}
</div>
</>
);
}
const renderFirstInputSet = () => {
if (props.firstInputValue)
return <FirstInputSet />
else
return null;
}
return (
<>
{renderFirstInputSet()}
</>
);
}
Neither props.printFirstArray nor props.printSecondArray is not returning anything props.printFirstArray 和 props.printSecondArray 都没有返回任何东西
From your code, FirstInputSet
is a new component and you aren't passing any props.从您的代码来看,
FirstInputSet
是一个新组件,您没有传递任何道具。
return <FirstInputSet />
Try to pass your props to the child component but I recommend keeping the child component out of parent component.尝试将您的道具传递给子组件,但我建议将子组件放在父组件之外。
OR或者
If you meant to use FirstInputSet
as a function, you can modify the code like below如果您打算使用
FirstInputSet
作为函数,您可以修改如下代码
return FirstInputSet();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.