1. 程式人生 > >React中reduxForm表單編輯

React中reduxForm表單編輯

reduxForm中反寫資料在輸入框中,資料是從別的模組拉取 .關鍵程式碼如下
// 編輯應用表單 
class EditCode extends React.Component {
  constructor(props) {
    super(props)
  }

  componentDidMount() { 

  }

  // 取消編輯
  handleBack=()=>{
    window.history.go(-1);
  }

  // 確定編輯
  handleFormSubmit=()=>{
    const { handleSubmit } = this.props;
    handleSubmit()
  }

  render() {
    const { handleSubmit, touched, error, pristine, reset, submitting, loading, initAppData } 
= this.props; return ( <div className="defence-detail" onSubmit={handleSubmit}> <div className='tc-panel'> <div className='tc-panel-bd'> <div className='param-box'> <div className='param-bd'> <ul className='form-list'> <li> <div className='form-label required'> <label htmlFor='Name'>應用名稱</label> </div> <div className="form-input"> <Field name='Name' placeholder='必填' component={inputAppName}/> </div> </li> <li> <div className='form-label required'> <label htmlFor='Developer'>應用開發商</label> </div> <div className='form-input'> <Field name='Developer' placeholder="必填" component={inputAppDeveloper}/> </div> </li> <li> <div className='form-label'> <label htmlFor='Desc'>應用描述</label> </div> <div className='form-input'> <Field name='Desc' component={textareaAppDescription}/> </div> </li> </ul> </div> </div> </div> <div className='tc-panel-ft'> <Button onClick={()=>this
.handleFormSubmit()} loading={loading}> 確認 </Button> <Button className='weak' onClick={()=>this.handleBack()} disabled={this.props.disabled}> 取消 </Button> </div> </div> </div> ) } } EditCode = reduxForm({ form:
'editAppForm',// 這是你的表單名稱 validate, enableReinitialize:true, keepDirtyOnReinitialize:true,// 這個值表示重新初始化表單後,不替換已更改的值,可以用clear來測試 })(EditCode) EditCode = connect( state => ({ initialValues: state.appManage.initAppData, //appManage是你需要拉取資料的模組 你需要填充的資料initAppData
}), )(EditCode) 
export
default EditCode

官網連結https://redux-form.com/6.7.0/examples/initializefromstate/