我在通过 connect和redux-form 一起使用 ref
时遇到问题。
组件的结构使得 ChildComponent 具有表单,而我正在使用
class EditEvent extends Component {
constructor (props) {
super(props);
this.handleStartDateChange = this.handleStartDateChange.bind(this);
this.handleEndDataChange = this.handleEndDataChange.bind(this);
this.state = {loading: false};
}
componentDidMount(){
this.props.fetchEvent(this.props.session,true);
this.props.fetchScheduleDates(this.props.session);
}
submitSettings(){
this.props.form.submit();
}
handleStartDateChange(date) {
this.props.updateUnsavedChanges();
this.props.dateRangeUpdate({startDate:date,endDate:date,allDate:{}});
}
handleEndDataChange(date) {
this.props.updateUnsavedChanges();
this.props.dateRangeUpdate({startDate:this.props.scheduledates.startDate,endDate:date,allDate:{}});
}
renderBlockList(startDate,endDate,allDate){
/*if(isEmpty(allDate)){
return 'Please select date duration first.';
}*/
var arr = [];
var day = {};
var n = endDate.diff(startDate,'days')+1;
for (var i=1; i <= n; i++ ) {
if(i !== 1){
day = moment(day).add(1,'days');
} else {
day = startDate;
}
var activeDate = _.find(allDate, {scheduledate:moment(day).format('YYYY-MM-DD')});
arr.push(<DateBlock key={i} day={day} activeDate={activeDate}/>);
}
return arr;
}
handleEventName(){
this.props.updateUnsavedChanges();
}
handleEventDesc(){
this.props.updateUnsavedChanges();
}
onFormSubmit(values){
debugger;
if (this.props.selectedFeatures.length === 0) {
this.props.showError('Select at least one feature!');
} else if (values.eventname === '') {
this.props.showError('Please enter a name');
} else {
this.props.submitSettingsHandler(values);
}
}
render(){
var eventStartDate = moment();
var eventEndDate = moment().add(6,'days');
var isDisabled = false;
var allDate = [];
if(this.props.scheduledates.startDate !== undefined){
eventStartDate = this.props.scheduledates.startDate;
eventEndDate = this.props.scheduledates.endDate;
allDate = this.props.allDate;
}
if(this.props.scheduledates.isEditable !== undefined){
isDisabled = true;
}
var {handleSubmit, pristine, submitting, invalid} = this.props;
return(
<form onSubmit={handleSubmit(this.onFormSubmit.bind(this))} encType="multipart/form-data">
<div className="general-tab-warp">
<Field
name="eventname"
label="Event name"
placeholder="E.g google I/O 2017"
type="text"
component={this.renderFieldText}
bsClass="row"
formdiv="false"
labelposition={LABEL_POSITION_TOP}
onChange={this.handleEventName.bind(this)}
/>
<Field
name="eventdesc"
label="Event Description"
placeholder="Enter event description"
type="text"
component={this.renderFieldTextarea}
bsClass="row"
formdiv="false"
labelposition={LABEL_POSITION_TOP}
onChange={this.handleEventDesc.bind(this)}
/>
<div className="form-group row">
<label className="control-label-top col-sm-12">Duration*</label>
<div className="col-sm-12">
<Row>
<Col md={6} style={{width:'48%'}}>
<DatePicker
selected = {eventStartDate}
className = "form-control"
dateFormat = "DD/MM/YYYY"
onChange = {this.handleStartDateChange}
name = "startdate"
selectsStart
startDate = {eventStartDate}
endDate = {eventEndDate}
minDate = {moment().add(1,'days')}
disabled={isDisabled}
/>
</Col>
<Col md={1} style={{padding:'0px',marginTop:'6px', width:'4%'}}>➔</Col>
<Col md={6} style={{width:'48%'}}>
<DatePicker
selected = {eventEndDate}
className = "form-control"
dateFormat = "DD/MM/YYYY"
onChange = {this.handleEndDataChange}
name = "enddate"
minDate = {eventStartDate}
maxDate = {moment(eventStartDate).add(6, "days")}
selectsEnd
startDate = {eventStartDate}
endDate = {eventEndDate}
disabled={isDisabled}
/>
</Col>
</Row>
<div className="duration-info">
* You can't update the date after publishing the app.
</div>
</div>
</div>
<div className="form-group row">
<label className="control-label-top col-sm-12">Days</label>
<div className="col-sm-12">
<ul className="event-days">
{this.renderBlockList(eventStartDate,eventEndDate,allDate)}
</ul>
</div>
</div>
<div className="form-group row">
<label className="control-label-top col-sm-12">Event Privacy</label>
<div className="col-md-12">
<FormGroup>
<Radio name="event_privacy" inline>
Public
</Radio>{' '}
<Radio name="event_privacy" inline>
Private
</Radio>
</FormGroup>
</div>
</div>
</form>
);
}
}
function mapStateToProps(state){
return{
features: state.features,
session: state.session,
initialValues: state.eventData[0],
scheduledates:state.scheduledates,
allDate:state.updateScheduleDates,
selectedFeatures: state.selectedFeatures
};
}
function validate(values) {
var errors = {};
if (values['eventname'] === '') {
errors['eventname'] = 'Event name is required';
}
return errors;
}
export default connect(mapStateToProps,{fetchEvent,updateUnsavedChanges,fetchScheduleDates,dateRangeUpdate,patchEventDetails,showError,showSuccess},null,{withRef:true})(reduxForm({
validate,
form:'EditEventForm',
enableReinitialize: true,
keepDirtyOnReinitialize: true
})(EditEvent));
该组件具有功能
submitSettings(){
this.props.form.submit();
}
我想从上级组件调用 submitSettings ,我正在其中使用 ref ,请参见下文
constructor(props){
super(props);
this.editEventRef = React.createRef();
}
onSaveClick(){
this.editEventRef.current.submitSettings();
}
render(){
return(
<EditEvent ref={this.editEventRef}/>
)
}
调用 onSaveClick 时,总是出现以下错误
未捕获的TypeError: this.editEventRef.current.submitSettings是 不是功能
错误发生了,当我更改以下行并删除了 reduxForm()
中的 connect()
时,该功能正常运行了>
export default
connect(mapStateToProps,
{fetchEventDetails}
,null,{withRef:true})(EditEvent);
有人可以指导我解决这个问题吗?我想念什么吗?
感谢帮助。
更新 这些是我正在使用的软件包的npm版本,如果有帮助的话。
“ react”:“ ^ 16.7.0”“ react-dom”:“ ^ 16.7.0”,“ react-redux”:“ ^ 5.1.1”, “ redux-form”:“ ^ 6.7.0”,
答案 0 :(得分:1)
好的,所以花了很多时间后,我发现解决方案对于“反应”:“ ^ 16.7.0” 和“ redux-form”:“ ^ 8.1”都是正确的。 0“ ,这是最新的。
在您以 connect() reduxForm()
的方式使用问题时,您需要使用
this.editEventRef.current.wrappedInstance.submitSettings();
因此,每当子组件中存在reduxForm时,您都需要使用 .current.wrappedInstance 来访问回调,否则仅使用 .current 。
您需要在{strong> connect()
中使用{forwardRef:true}
作为选项
除了上面的作品外,我没有发现它的任何地方。
希望它可以帮助寻找相似答案的人。
干杯。