从状态react redux检索属性值

时间:2018-03-23 09:45:56

标签: reactjs redux redux-form

我更新了状态,但无法检索特定密钥的值 。我想打印像user_added

这样的结果
import React from 'react';
import {Field , reduxForm} from 'redux-form';
import {addUser} from '../actions/index';
import FlatButton from 'material-ui/FlatButton';
import {connect} from 'react-redux';
import TextField from 'material-ui/TextField';
import {RadioButtonGroup , RadioButton} from 'material-ui/RadioButton';
import Result from './User_add_result'

export class Register extends React.Component {
    constructor(props){
        super(props);
        this.state = {
            role: "",
            username: "",
            email: "",
            token: "",
            password: "",
            userData : ""
        }
        this.handleSubmit = this.handleSubmit.bind(this);
    }

    render() {
       return (
           <div>
           {console.log('result',this.props.userData)}
           {/* {this.props.userData[0]} */}
           {/* {this.props.userData.map(([key , value]) => <p>{value.toString()}</p>)} */}
           {/* <Result result={this.props.userData} /> */}
           </div>
      );
    }
}

const mapStateToProps = (state) => {
    console.log("user result ==========>>>>", state.user[0]);    
    return {
        userData: state.user
    }
}

我得到的结果如下:

result [{…}]0: result: "user_added"__proto__: Objectlength: 1__proto__: Array(0)

2 个答案:

答案 0 :(得分:1)

我得到了答案 只需要映射单个输出

{ this.props.userData && this.props.userData.map( (user) => user.result)}

答案 1 :(得分:1)

我认为推荐的方法是使用Redux Form附带的formValueSelector方法。