简体   繁体   English

为什么我不能直接在子组件中访问道具?

[英]Why can't I access the props directly in Child Component?

I have stored the data in a file (details.js) as a JavaScript Object, then I'm trying to pass it as a prop from MainComponent to ChildComponent.我已将数据作为 JavaScript Object 存储在文件(details.js)中,然后我试图将其作为道具从 MainComponent 传递给 ChildComponent。 Well the prop is accessible in ChildComponent but I could access it with props.ChildDetails[0].name instead of props.name directly as shown in the Official documentation好吧,道具可以在 ChildComponent 中访问,但我可以使用props.ChildDetails[0].name而不是props.name直接访问它,如官方文档中所示

What's incorrect in my code?我的代码有什么不正确的地方?

details.js细节.js

export const DETAILS=[
    {
        id:0,
        profile:'/assests/images/john.png',
        name:'John Doe',
    }
]

MainComponent.js MainComponent.js

import React,{useState} from 'react';
import Child from './ChildComponent';
import {DETAILS} from '../Shared/details';
function MainComponent(){
    return(
        <>
            <Child ChildDetails={DETAILS}/>
        </>
    );
}
export default MainComponent;

ChildComponent.js ChildComponent.js

import React from 'react';
function ChildComponent(props){
    console.log(props.name) //Output:undefined
    console.log(props.ChildDetails[0].name) //Output:John Doe
    return(
        <div></div>
    );
}

export default ChildComponent;

Because Details is object.因为详细信息是 object。 Try something like:尝试类似:

function ChildComponent({ ChildDetails }){
    const { name } = ChildDetails[0]
    ...
}

That's not even React.这甚至不是 React。 That's JavaScript那是 JavaScript

Edit .编辑. Oops.哎呀。 People said that's an array.人们说那是一个数组。 I thought it's just an object.我以为它只是一个 object。 Fixed code a bit固定代码一点

here is where you passed the object这是您通过 object 的地方

<Child ChildDetails={DETAILS}/>

and here is how you tried to use it这是您尝试使用它的方式

console.log(props.name) //Output:undefined

since you defined the name as ChildDetails there is no such thing as props.name instead you must say由于您将名称定义为ChildDetails ,因此没有 props.name 这样的东西,您必须说

console.log(props.ChildDetails)

and if you want to get access to an name you should declare the index if not react does not know which index you want to use, so as you did before如果你想访问一个name ,你应该声明索引如果没有反应不知道你想使用哪个索引,就像你之前做的那样

 props.ChildDetails[0].name

暂无
暂无

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

相关问题 为什么我不能访问我的组件中的道具? - Why can't I access props within my component? React 父组件道具无法在子组件中使用 forwardRef 道具访问 - React parent component props can't access using forwardRef props in child component Vue.js:为什么我不能将外部属性绑定到子道具组件? - Vue.js : Why I can't bind external property to child props component? 为什么我不能调用作为道具传递给子组件的父 function? - Why can't I call the parent function passed as props to child component? 为什么我不能在事件处理程序中访问我的功能组件的道具? - Why can't I access my functional component's props within an event handler? 无法解构子组件中的道具 - Can't destructure props in child component ReactJS:子组件中未定义道具,我无法通过父组件传递道具参数 - ReactJS: Props is undefined in child component and I can't pass props parameters throught parent 为什么我不能将我的道具发送到组件? - Why can't I send my props to a component? 为什么我不能直接修改一个组件的 state,真的吗? - Why can't I directly modify a component's state, really? 如果我可以直接访问和使用this.props和this.state,那为什么我必须在react native中通过构造函数中的super(props)调用它们? - If i can access and use the this.props & this.state directly then why i have to call them via the super(props) in constructor in react native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM