简体   繁体   English

解构道具 object 返回 undefined

[英]Destructuring a props object returns undefined

I'm trying to create a data-grid in react, and I've been having trouble with accessing the data in my props.我正在尝试在反应中创建一个数据网格,并且在访问我的道具中的数据时遇到了麻烦。 Everytime I try to access the data or destructure the prop my console reads "undefined".每次我尝试访问数据或解构道具时,我的控制台都会显示“未定义”。

I've only been getting this error when the prop is an object of arrays (of objects). 当道具是 arrays 的 object (对象)时,我才收到此错误。 When, doing the same thing with just an object of strings, it seems to be working fine - so I feel like I'm missing something fundamental here. 当只使用 object 字符串做同样的事情时,它似乎工作正常 - 所以我觉得我在这里缺少一些基本的东西。

In the following extracts, I've removed some code for brevity.在以下摘录中,为简洁起见,我删除了一些代码。 In one file, I have the following:在一个文件中,我有以下内容:

let inputRows = [
    { id: 0, title: "Task 1", complete: 20 },
    { id: 1, title: "Task 2", complete: 40 },
    { id: 2, title: "Task 3", complete: 60 }];
let inputCols = [
    { key: "id", name: "ID", editable: true },
    { key: "title", name: "Title", editable: true },
    { key: "complete", name: "Complete", editable: true }];
let matrixExample1 = {inputRows, inputCols};

const Tabs=(props) => {
    return (
        <div>
            <MatrixParameter props={matrixExample1}>
        <div>
    );

In the MatrixParameter file, I have the following:在 MatrixParameter 文件中,我有以下内容:

const MatrixParameter = (props) => {
    console.log(props);

    let {
        inputRows,
        inputCols
    } = props;

    console.log(props);
    console.log(props.inputRows);
    console.log(inputRows);

    return (
        <*removed*>
    )

Console.log returns the following: reading the props successfully for the first two logs, but returning undefined for the next two logs. Console.log 返回以下内容:成功读取前两个日志的道具,但返回未定义的下两个日志。 The removed portion of my code returns an error, saying that inputRows is undefined.我的代码中删除的部分返回一个错误,说 inputRows 是未定义的。 What's going on?这是怎么回事?

( edit: As suggested by the answers, I wasn't doing the same thing for the object of strings. The error was the incorrect passing of the object by using <MatrixParameter props="matrixExample1"/> and/or the failure to compensate for that by using (props) instead of ({props} ). 编辑:正如答案所建议的那样,我对字符串的 object 没有做同样的事情。错误是使用<MatrixParameter props="matrixExample1"/>和/或未能补偿 object 的错误传递为此,使用 (props) 而不是 ({props} )。

As explained in the comments and other others, <MatrixParameter props={matrixExample1}> causes the argument of the functional component (named props ) to be an object with a .props property.正如评论和其他内容中所解释的那样, <MatrixParameter props={matrixExample1}>导致功能组件(名为props )的参数是具有.props属性的 object 。

However, instead of changing the destructuring to work with that, I think what you actually meant to do was using jsx spread attributes但是,我认为您实际上要做的是使用 jsx 传播属性,而不是更改解构以使用它

<MatrixParameter {...matrixExample1} />

which is equivalent to这相当于

<MatrixParameter inputRows={inputRows} inputCols={inputCols} />

you have passed them as an object with the key value of props .您已将它们作为 object 传递,键值为props so it should be destructed as below.所以它应该被破坏如下。

let {
        inputRows,
        inputCols
    } = props.props;

You can also use this method of destruction:你也可以使用这种销毁方法:

   let {
        props:{
        inputRows,
        inputCols
        }
    } = props;

you are passing props as prop of MatrixParameter to fix your code you need to use it like您正在传递道具作为 MatrixParameter 的道具来修复您需要使用它的代码

props.props 
 //or like this 
MatrixParameter = ({props})=>

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

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