简体   繁体   English

无法访问功能组件的函数内的道具

[英]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.

相关问题 无法从 React 组件中的 function 访问道具 - Cannot access props from function inside React Component 功能组件中的默认功能道具 - Default function props in functional component 无法读取功能组件中未定义的属性“道具” - Cannot read property 'props' of undefined in functional component React Redux 无法传递功能组件的 props - React Redux cannot pass props of a functional component 在功能组件中访问单独的 function 中的道具 - Accessing props in separate function in a functional component 我收到 TypeError:尝试在 React 的功能组件中使用道具时无法设置未定义的属性“道具”? - I'm getting TypeError: Cannot set property 'props' of undefined while trying to use props inside functional component in React? 如何访问 getServerSideProps() function 中组件的道具? - How can I get access to props of a component inside the getServerSideProps() function? 在组件函数“无法读取未定义的属性&#39;props”中调用props函数 - Calling props function inside component's function “Cannot read property 'props' of undefined” 通过具有功能组件的道具,而无需在组件内声明功能 - pass props with functional component without declaring function within component 功能组件中的道具 - props in functional component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM