【发布时间】:2019-11-10 09:03:26
【问题描述】:
它正在工作。
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
【问题讨论】:
-
请在沙箱中分享您的代码。它显示了什么错误?
-
对不起,我不知道如何在沙盒中分享这些大代码。它还在后端有 nodeJS 与 MySql DB 连接。它没有任何错误。只显示表单的空白字段。
-
请看this.state.userDatascreencast.com/t/surMy6QwwP的数据日志
标签: reactjs redux react-redux redux-form