我一直在嘗試使用Redux-Form在MapsAddrForm.jsx中實現一個表單,而且我似乎無法更改我的輸入元素的值。當頁面加載時,輸入元素不響應鍵盤輸入,並且當表單字段提交時,它將一個空對象返回給父組件區域查找器。除了這兩個文件之外,我還添加了form:formReducer作爲組合縮減器的參數,就像Redux-Form教程中的簡單示例一樣。有沒有辦法恢復DistrictFinder從地址表單接收數據對象的能力?作爲參考,我使用React 15.1.0,React-redux 4.4.5,ES6和Redux-Form 5.3.1,全部使用Webpack編譯。Redux-Form:無法更改輸入元素的值
MapsAddrForm.jsx
import React, {Component} from 'react';
import { connect } from 'react-redux';
import {reduxForm} from 'redux-form';
class MapsAddrForm extends Component {
constructor(props) {
super(props);
}
render() {
const {fields: {address,address2}, handleSubmit} = this.props;
return (
<form onSubmit={handleSubmit}>
<div>
<input type="text" placeholder="Your address" {...address}/>
</div>
<button type="submit">Submit</button>
</form>
);
}
}
export default reduxForm({
form: 'addressForm',
fields: ['address']
})(MapsAddrForm);
DistrictFinder.jsx
import React, { Component, PropTypes } from 'react'
import MapsAddrForm from './MapsAddrForm.jsx'
import { connect } from 'react-redux'
import { changeAddress } from '../actions/index.jsx'
class DistrictFinder extends Component {
constructor(props) {
super(props);
this.handleAddrSubmit = this.handleAddrSubmit.bind(this);
}
handleAddrSubmit(data) {
console.log("Address received: " + JSON.stringify(data));
}
render() {
const {address, district} = this.props
return (
<div class="GMaps">
<h1>Find your district!</h1>
<MapsAddrForm onSubmit={this.handleAddrSubmit} />
<p>My district number is: {district}</p>
</div>
);
}
}
DistrictFinder.propTypes = {
district: PropTypes.string.isRequired,
dispatch: PropTypes.func.isRequired
};
function mapStateToProps(state) {
const { district } = state.infoChange;
return {
district
};
};
export default connect(mapStateToProps)(DistrictFinder);