2016-05-30 6 views
2

Ich mache einfache Blog mit react redux. Als Paket verwende ich redux Form Ich machte Ereignisse wie Post, erhalten, lösche aber ich konnte Form nicht bilden, weil ich nicht Werte Titel und Körper in redigieren kann. Ich habe versucht, es zu lösen mit in componentwillMount initialisieren, aber es ist Fehler zu bekommen kann nicht lesen Eigenschaft ‚title‘ undefinierter wenn ich this.props.edit.title in ComponentWillMount schreibenWie kann ich Post-Werte in Bearbeitung mit React/Redux-Formular in React

Wie kann ich dieses Problem zu lösen, Wie kann ich Werte im Bearbeitungsformular

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); 
+0

'this.props.edit' ist wahrscheinlich nicht definiert. Wie gibst du diese Requisiten? Kann ich Ihren Reduzierer sehen? Kannst du etwas mehr Code posten? – glcheetham

+0

Beachten Sie auch, dass Sie bei Verwendung der ES6-Klassensyntax einen Konstruktor anstelle von 'componentWillMount' verwenden sollten – glcheetham

+0

hi, @glcheetham für Ihre Antwort, löste ich das Problem auf folgende Weise. – onerciller

Antwort

2

Ich löste Problem in folgender Weise. Ich kann den Postwert mit Ausgangsw erhalten: state.posts.edit

function mapStateToProps(state) { 
    return { 
     edit:state.posts.edit, 
     initialValues: state.posts.edit 
    } 
} 
+0

http://stackoverflow.com/questions/43146388/editable-form-reactjs – Piyush