gpt4 book ai didi

javascript - redux-form 中的同名字段

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:57:12 24 4
gpt4 key购买 nike

我正在处理评论部分并使用 redux-form 来获取输入。我只有一个 Field 组件用于接收输入。但是当我在输入框中键入时,所有输入框都会填充相同的值。我知道这是因为所有这些输入框都具有相同的名称。但是我只想将值填充到选中的输入框。我该怎么做?

这是我的组件:

import { compose } from "redux";
import { connect } from "react-redux";
import React, { Component } from "react";
import { Field, reduxForm } from "redux-form";

import { addComment, fetchPosts } from "../../../actions/FeedPost";
import "./FeedCommentsInput.css";
import TextareaAutosize from "react-textarea-autosize";

class FeedCommentsInput extends Component {
renderField = field => {
const { touched, error } = field.meta;
const className = `comment-input-box ${
touched && error ? "red-border__error-comment" : ""
}`;
return (
<TextareaAutosize
{...field.input}
placeholder={field.placeholder}
type={field.type}
className={className}
/>
);
};

onSubmit = formProps => {
const { postid } = this.props;
this.props.addComment(formProps, postid);
};

render() {
const { handleSubmit } = this.props;

return (
<div>
<form onSubmit={handleSubmit(this.onSubmit)}>
<Field
name="text"
component={this.renderField}
type="text"
placeholder="Enter your Comment"
/>
<button type="submit">Submit</button>
</form>
</div>
);
}
}

const validate = values => {
const errors = {};

if (!values.comment) errors.comment = "Please Enter Something";

return errors;
};

export default compose(
connect(
null,
{ addComment, fetchPosts }
),
reduxForm({ validate, form: "commentbox" })
)(FeedCommentsInput);

最佳答案

只需将唯一的 form 属性传递给您的 Form 组件。

您的 redux-form 集成将更改为:

// No need to configure `form`, because it would be set dynamically via props
reduxForm({ validate })

用法:

<Form form={`commentbox_${uuid}`} />

Credits .

关于javascript - redux-form 中的同名字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52946335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com