React Redux:this.props有限吗?

时间:2017-07-24 09:59:31

标签: javascript reactjs redux react-redux

我正在使用react和redux。

我真的不明白这个简单的问题:我用this.prop来检索一些数据,但问题是:

当我输入:

console.log(this.props.activeDevice.device) 

它返回我想要的元素:

Object { name: "InputTwo", deviceType: "Input", value: "21", description: "This is the second Input" }

然后,如果我想检索名称值,我应该输入:

console.log(this.props.activeDevice.device.name)

但是我收到了这个错误:

TypeError: this.props.activeDevice.device is undefined 

enter image description here

而this.props.activeDevice.device就在你看到的第一个console.log中上面定义。

这里是我使用控制台日志的代码:

import React, { Component } from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';


class DeviceConfig extends Component {

    render() {

        console.log(this.props.activeDevice.device); // this line returns me Object { name: "InputTwo", deviceType: "Input", value: "21", description: "This is the second Input" }

        // console.log(this.props.activeDevice.device.deviceType); // if I comment the first log and use this log, it returns me the error no matter if i target deviceType, name, value of the object

        switch(this.props.activeDevice.device.deviceType){

            case "Input":
                return (
                    <div>
                        <table>
                            <tbody>
                                <tr>
                                    <td className="selected_DeviceName"> </td>
                                    <td> <input type="number" ref="inputConfig" defaultValue="" /> </td>
                                    <td className="sendButton"> Send </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>                  
                );

/////////////////////////////////////////
            default:
                return (
                    <p> Select a Device </p>
                );


        }
  }
}

function mapStateToProps(state){
    return{
            activeDevice:state.activeDevice,
    };
}


export default connect(mapStateToProps)(DeviceConfig); 

activeDevice链接到以下操作:

 export const selectDevice = (device,index) => {
    console.log("Device Clicked : " + device.value + ":" + index);
    return {
        type: "DEVICE_SELECTED",
        payload: {device,index}
    }
};

但是,如果我使用这个有效载荷:

payload: device

我可以使用例如this.props.activeDevice.name或this.props.activeDevice.deviceType。问题可能是有效负载语法?我在第一种情况下有2个对象,其中一个是对象,在第二种情况下我只有对象设备。

提前致谢...

0 个答案:

没有答案