无法将数据库值传递到Redux表单

路加

这是工作。

var userData={
            first_name : "My First Name",
            last_name : "My Last Name",
            email : "My Email"
        };

但这不起作用

var userData={
            first_name : this.state.userData.first_name,
            last_name : this.state.userData.last_name,
            email : this.state.userData.email
        };

this.state.userData具有用户数据。它显示在日志中。这是完整的代码。

import React, {Component} from 'react';
import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import store, {history} from '../../store/configureStore';

import {CLIENTS} from '../../constants/entity'
import * as crudAction from '../../actions/crudAction'

// Import custom components
import AddClientForm from '../../components/client/AddClientForm.js';
import {httpBase} from '../../utils/httpBaseUtil';

class AddClientContainer extends React.Component {
    constructor(props) {
        super(props);

        this.submitForm = this.submitForm.bind(this);
        this.state = {
            isFetching: true,
            userData: []
        };
    }

    componentDidMount(){
        httpBase().get('clients/'+2)
            .then((response) => {
                this.setState({ userData: response.data.data, isFetching: false })
            })
            .catch((error) => {
                console.log('Error: ',error);
            });

        this.setState({isFetching: false })
    }


    /**
     * Submit the form.
     *
     * @param {object} formProps
     */
    submitForm(formProps) {

        this.props.actions.submitForm(CLIENTS, formProps);
    }

    
    render() {
        if (this.state.isFetching){
            return(<p> Loading...</p>)
          }
        
        var userData={
            first_name : this.state.userData.first_name,
            last_name : this.state.userData.last_name,
            email : this.state.userData.email
        };
       
        return (
            <AddClientForm
                initialValues={userData}
                onSubmit={this.submitForm}
            />
        );
    }

}

/**
 * Map the actions to props.
 */
const mapDispatchToProps = dispatch => ({
    actions: bindActionCreators(Object.assign({}, crudAction), dispatch)
});

export default connect(null, mapDispatchToProps)(AddClientContainer)

注意:我的应用程序基于此样板。https://github.com/Bikranshu/express-react-boilerplate

奥赞(Ozan Bulut)

在构造函数中,将其设置userData为空数组,并在构造函数中将其设置isFetching为true。

然后,您的组件将挂载并调用componentDidMount,从而使API调用设置isFetching为true。此时,您的API调用可能未完成,这意味着isFetching为false,并且userData为空数组。

只需删除componentDidMount方法中的最后一行,userData由于其类型实际上是对象,我也不会初始化为空数组。

 componentDidMount(){
    httpBase().get('clients/'+2)
        .then((response) => {
            this.setState({ userData: response.data.data, isFetching: false })
        })
        .catch((error) => {
            console.log('Error: ',error);
        });

    this.setState({isFetching: false }) // Remove this line.
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将初始数据从数据库传递到Django表单向导模板

来自分类Dev

PHP HTML表单无法将数据发布到数据库?

来自分类Dev

从MVC表单将数据发布到数据库

来自分类Dev

使用PHP将数据从HTML表单传递到MySQL数据库

来自分类Dev

无法将隐藏的表单值从数据库传递给PHP if语句

来自分类Dev

将多个行值从数据库传递到XML

来自分类Dev

使用PHP表单将值存储到数据库中

来自分类Dev

如何将值从数据库传递到php中的弹出窗口?

来自分类Dev

将值传递给数据库

来自分类Dev

将值从单选按钮传递到数据库

来自分类Dev

Laravel:无法将数组从数据库从路由器传递到视图

来自分类Dev

将数据库值传递到布局

来自分类Dev

从MVC表单将数据发布到数据库

来自分类Dev

使用PHP将数据从HTML表单传递到MySQL数据库

来自分类Dev

如何使用AJAX将值传递到数据库

来自分类Dev

无法在执行插入到数据库的代码后面的代码中获取表单值

来自分类Dev

如何使用PDO将Bootstrap模态表单值发布到MYSQL数据库

来自分类Dev

使用JavaScript将值从dropdownlist(绑定到数据库)传递到文本框

来自分类Dev

无法将值插入数据库

来自分类Dev

如何使用OOP概念使用ArrayList对象将值传递到数据库中

来自分类Dev

如何将表单中的值存储到mySQL数据库中?

来自分类Dev

PHP表单仅将图像发布到MYSQL数据库,没有其他值

来自分类Dev

如何将值从数据库传递到php中的弹出窗口?

来自分类Dev

如何使用Sinatra和Datamapper将数组从表单传递到数据库?

来自分类Dev

将值传递给数据库

来自分类Dev

将数据从“HTML”登录表单传递到“PHP”以从“MySQL”数据库中检索数据

来自分类Dev

使用 wordpress 4.7 将表单值插入到数据库表中

来自分类Dev

无法将表单数据传递给 PHP 以更新数据库

来自分类Dev

无法将表单数据发布到 RDS MySQL 数据库

Related 相关文章

  1. 1

    将初始数据从数据库传递到Django表单向导模板

  2. 2

    PHP HTML表单无法将数据发布到数据库?

  3. 3

    从MVC表单将数据发布到数据库

  4. 4

    使用PHP将数据从HTML表单传递到MySQL数据库

  5. 5

    无法将隐藏的表单值从数据库传递给PHP if语句

  6. 6

    将多个行值从数据库传递到XML

  7. 7

    使用PHP表单将值存储到数据库中

  8. 8

    如何将值从数据库传递到php中的弹出窗口?

  9. 9

    将值传递给数据库

  10. 10

    将值从单选按钮传递到数据库

  11. 11

    Laravel:无法将数组从数据库从路由器传递到视图

  12. 12

    将数据库值传递到布局

  13. 13

    从MVC表单将数据发布到数据库

  14. 14

    使用PHP将数据从HTML表单传递到MySQL数据库

  15. 15

    如何使用AJAX将值传递到数据库

  16. 16

    无法在执行插入到数据库的代码后面的代码中获取表单值

  17. 17

    如何使用PDO将Bootstrap模态表单值发布到MYSQL数据库

  18. 18

    使用JavaScript将值从dropdownlist(绑定到数据库)传递到文本框

  19. 19

    无法将值插入数据库

  20. 20

    如何使用OOP概念使用ArrayList对象将值传递到数据库中

  21. 21

    如何将表单中的值存储到mySQL数据库中?

  22. 22

    PHP表单仅将图像发布到MYSQL数据库,没有其他值

  23. 23

    如何将值从数据库传递到php中的弹出窗口?

  24. 24

    如何使用Sinatra和Datamapper将数组从表单传递到数据库?

  25. 25

    将值传递给数据库

  26. 26

    将数据从“HTML”登录表单传递到“PHP”以从“MySQL”数据库中检索数据

  27. 27

    使用 wordpress 4.7 将表单值插入到数据库表中

  28. 28

    无法将表单数据传递给 PHP 以更新数据库

  29. 29

    无法将表单数据发布到 RDS MySQL 数据库

热门标签

归档