反应未定义的子组件接收道具 [英] React child component receiving props as undefined

查看:63
本文介绍了反应未定义的子组件接收道具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有组件Comp1,它是子组件Comp2. markers的状态在该对象的父组件中设置:

I have component Comp1 and it's child Comp2. The state of markers is set in the parent component to this object:

    var MARKER_ITEMS = {
            "item1" : {
                  "cat" : "wow",
                  "img" : "slide",
                  "info" : "bike",
                  "key" : "1"
                },
                "item2" : {
                  "cat" : "omg",
                  "img" : "slide",
                  "info" : "4x4",
                  "key" : "2"
                }
        };

当我尝试使用loadash _.map为每个对象生成一个Comp2时,道具会以未定义的形式传递下来.

When I try to generate a Comp2 for each object with loadash _.map, the props get passed down as undefined.

jsfiddle

推荐答案

您的代码有效,但是您将key名称用作道具. key是一个特殊的保留属性名称,React使用该名称来知道组件具有哪些动态子级.

Your code works but you're using the key name as a props. key is a special reserved property name that React uses in order to know which dynamic children a component has.

例如,如果从key切换到stuff,您的代码将起作用.

If you switch from key to stuff for example, your code will work.

在此处阅读此处

这篇关于反应未定义的子组件接收道具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆