我对如何通过分派传递textarea值并保存到存储有疑问。
我的减速器是这样的:
var defaultCommentState = {
login_status: false,
author_name: '',
author_email: '',
author_url: 'https://google.com',
content: '',
post:null,
parent:0
}
const addComment = (state = defaultCommentState, action) => {
switch(action.type) {
case 'ADD_COMMENT_CONTENT':
return {
...state,
content: action.content
}
default:
return state;
}
}
export default addComment;
我的表单如下:
<form name="post_comment" className="post-a--comment" onSubmit={this.handleSubmit}>
<div className="thecmstatus" />
<label>Message (You signed in as <span>{author_name}</span>)</label>
<textarea id="content"
onChange={this.handlecontent}
value={this.state.content}
required="required" /> <br/>
<button type="submit"
id="blog_post_submit"
className="btn-default btn">
Submit
</button>
</form>
我的动作是这样的
const addcommentcontent = (content) => {
return {
type: 'ADD_COMMENT_CONTENT',
content
}
}
我可以使用onChange
函数调度状态更改,但这是个好方法。
handleSubmit = (e) => {
e.preventDefault();
this.setState({
...this.props.addComment,
content:this.props.content
})
this.props.onSubmit(this.props.addComment);
}
handlecontent = (e) => {
this.setState({
content: e.target.value
})
//I dispatch on state change but I think it is not good
const {commentcontentadded} = this.props;
commentcontentadded(this.state.content);
}
您能告诉我如何通过调度将textarea值传递给存储,因为我需要从存储中提交有效负载,而不是像这样的状态。
this.props.onSubmit(this.props.addComment)
答案 0 :(得分:-1)
导入动作创建者(addcommentcontent),并通过handleSubmit方法调度他。
handleSubmit = (e) => {
e.preventDefault();
this.setState({
...this.props.addComment,
content:this.props.content
})
dispatch(addcommentcontent(this.state.content));
}