enter image description here enter image description here我正在撰写博客页面,您可以点击按钮并打开模式以编辑帖子下的评论。
在帖子详细信息页面上我有:
openEditCommentModal = (id) => {
this.setState(() => ({
editCommentModalOpen: true,
}))
this.props.fetchCommentById(id)
}
在编辑评论文件中我有:
const mapStateToProps = state => {
return {
initialValues: state.commentContainer.selectedComment
}
}
所以我做了两件事: 开放模式 2.调用fetchCommentById(id)以便我可以更新state.commentContainer.selectedComment
帖子详细信息页面是父组件,编辑评论表单是子组件。
我需要将initialValue提供给编辑表单。 initialValue是评论的原始内容,然后用户可以编辑它。
奇怪的是,当我第一次点击编辑按钮时,没有initialValue。它是一个空物。因此,我有一个空的表格。 当我关闭模态并重新打开它时,initialValue正在更新为我想要的注释,编辑注释表单现在具有所有原始内容。
我不明白为什么openEditCommentModal第一次无法更新状态。有人可以帮忙吗?
==============
问题解决了: 在reduxForm中,执行以下操作:
enableReinitialize:true
在帖子详细信息页面:
openEditCommentModal = (id) => {
this.setState(() => ({
editCommentModalOpen: true,
}))
this.props.fetchCommentById(id)
}
.........
<Modal
className='modal'
overlayClassName='overlay'
isOpen={editCommentModalOpen}
onRequestClose={this.closeEditCommentModal}
contentLabel='Modal'
>
<div>
<EditComment />
<button onClick={() => this.closeEditCommentModal()}>Close</button>
</div>
</Modal>
...............
const mapDispatchToProps = dispatch => {
return {
fetchByPostId: id => dispatch(fetchByPostId(id)),
deleteByPostId: id => dispatch(deleteByPostId(id)),
deleteByCommentId: id => dispatch(deleteByCommentId(id)),
vote: (id, option) => dispatch(vote(id, option)),
fetchComments: id => dispatch(fetchComments(id)),
fetchCommentById: id => dispatch(fetchCommentById(id))
};
};
编辑评论组件:
import React, { Component } from "react";
import { Field, reduxForm } from 'redux-form'
import { Link } from 'react-router-dom'
import { connect } from 'react-redux'
import { fetchCommentById } from '../Actions'
// import { updateComment } from '../Actions'
const randomID = require("random-id")
class EditComment extends Component {
renderField = (field) => {
const { meta: { touched, error } } = field
const className = `form-group ${ touched && error ? 'has-danger' : '' }`
return (
<div className={className}>
<label>{field.label}</label>
<input
className="form-control"
type="text"
{...field.input}
/>
<div className="text-help">
{touched ? error : ''}
</div>
</div>
)
}
onSubmit = (values) => {
const id = values.id
let newValues = {}
newValues.body = values.body
// this.props.updatePost(newValues, id, this.props.history.push(`/post/${id}`) )
}
render() {
console.log('this.props.initialValue', this.props.initialValues)
if(!this.props.initialValues) {
return <div></div>
}
const { handleSubmit, categories, id } = this.props
return (
<div>
<h1>Edit Comment</h1>
<form onSubmit={handleSubmit(this.onSubmit)}>
<Field
label="Content"
name="body"
component={this.renderField}
/>
<span>Author: </span><span>{this.props.initialValues.author} </span>
<span>Time: </span><span>{this.props.initialValues.timestamp}</span>
<div>
<button type="submit">Submit</button>
</div>
</form>
</div>
)
}
}
const validate = (values) => {
const errors = {}
if(!values.title) {
errors.title = "Enter a title"
}
if(!values.category) {
errors.category = "Enter a category"
}
if(!values.content) {
errors.content = "Enter some content"
}
return errors
}
const mapStateToProps = state => {
return {
initialValues: state.commentContainer.selectedComment
}
}
EditComment = reduxForm({
validate,
form: 'CommentEditForm',
enableReinitialize : true
})(EditComment)
export default connect(mapStateToProps, { fetchCommentById })(EditComment)
答案 0 :(得分:0)
我检查了您的代码并搜索了您的问题,我发现这个answer您必须启用表单初始化以与您的数据绑定,以便您需要执行以下操作:
EditComment = reduxForm({
validate,
form: 'CommentEditForm',
enableReinitialize : true // you need to add this property
})(EditComment)