Javascript 如何使用React中的React/Redux表单在编辑中获取post值
我正在用react redux制作简单的博客。作为软件包,我使用的是redux表单 我制作了诸如post、get、delete之类的事件,但我无法形成编辑,因为我无法在编辑中获取标题和正文的值。我试图用componentwillMount中的initialize来解决这个问题,但当我在componentwillMount中写入this.props.edit.title时,无法读取未定义的属性“title”时出现错误 如何解决这个问题,如何以编辑形式获取值Javascript 如何使用React中的React/Redux表单在编辑中获取post值,javascript,reactjs,redux,react-redux,redux-form,Javascript,Reactjs,Redux,React Redux,Redux Form,我正在用react redux制作简单的博客。作为软件包,我使用的是redux表单 我制作了诸如post、get、delete之类的事件,但我无法形成编辑,因为我无法在编辑中获取标题和正文的值。我试图用componentwillMount中的initialize来解决这个问题,但当我在componentwillMount中写入this.props.edit.title时,无法读取未定义的属性“title”时出现错误 如何解决这个问题,如何以编辑形式获取值 import React, { Comp
import React, { Component, PropTypes } from 'react';
import * as actions from '../../actions/index';
import { connect } from 'react-redux';
import {reduxForm} from 'redux-form';
import {initialize} from 'redux-form';
class EditPost extends Component {
componentWillMount(){
this.props.dispatch(initialize('edit', { title: this.props.edit.title }, ['title', 'body']));
this.props.EditPost(this.props.params.id);
}
handleFormSubmit(formProps){
this.props.addPost(formProps);
this.context.router.push('/posts');
}
render(){
const {handleSubmit,fields:{title,body}} = this.props;
if(!this.props.edit){
return <div>Loading...</div>;
}
return (
<div>
<div className="row">
<div className="col-md-12">
<form onSubmit={handleSubmit(this.handleFormSubmit.bind(this))}>
<fieldset className="form-group">
<label>Title:</label>
<input {...title} className="form-control" />
{title.touched && title.error && <div className="text-danger">{title.error}</div>}
</fieldset>
<fieldset className="form-group">
<label>Body:</label>
<textarea {...body} className="form-control" ></textarea>
{body.touched && body.error && <div className="text-danger">{body.error}</div>}
</fieldset>
<button className="btn btn-success">Add</button>
</form>
</div>
</div>
</div>
);
}
}
function mapStateToProps(state) {
return {
edit:state.posts.edit
}
}
export default reduxForm({
form:'edit',
fields:['title','body'],},mapStateToProps,actions)(EditPost);
import React,{Component,PropTypes}来自'React';
从“../../actions/index”导入*作为操作;
从'react redux'导入{connect};
从'redux form'导入{reduxForm};
从'redux form'导入{initialize};
类EditPost扩展组件{
组件willmount(){
this.props.dispatch(初始化('edit',{title:this.props.edit.title},['title',body']);
this.props.EditPost(this.props.params.id);
}
handleFormSubmit(formProps){
this.props.addPost(formProps);
this.context.router.push('/posts');
}
render(){
const{handleSubmit,字段:{title,body}}=this.props;
如果(!this.props.edit){
返回装载。。。;
}
返回(
标题:
{title.toucted&&title.error&&{title.error}
正文:
{body.toucted&&body.error&&{body.error}
添加
);
}
}
函数MapStateTops(状态){
返回{
编辑:state.posts.edit
}
}
导出默认reduxForm({
表格:'编辑',
字段:['title','body'],},mapStateToProps,actions)(EditPost);
我用下面的方法解决了这个问题。我可以使用initialValues:state.posts.edit来获取post值
function mapStateToProps(state) {
return {
edit:state.posts.edit,
initialValues: state.posts.edit
}
}
此.props.edit
可能未定义。你怎么通过这些道具?我能看看你的减速机吗?你可以发布更多的代码吗?还要注意,当使用ES6类语法时,你应该使用构造函数,而不是componentWillMount
hi,@glcheetham为你的回复,我用以下方法解决了这个问题。