gpt4 book ai didi

reactjs - react 最终形式重置为初始值

转载 作者:行者123 更新时间:2023-12-05 02:00:23 30 4
gpt4 key购买 nike

我对 React final form 有问题,它在 3 秒后重置值并恢复到初始值......调试了 3 天但没有任何变化......这是代码......任何想法和信息都会受到影响,谢谢

const ChoiceQuestionFrom = ({ editMode = true, onSubmit = () => {}}) => {
const handleValidations = (values) => {
let errors = {};
if(!values.question){
errors.question = "Question Required !"
}
return errors;
}
return (
<Form
onSubmit={(values) => {
return onSubmit(values);
}}
render={_ChoiceQuestionFrom}
editMode={editMode}
initialValues={{
type: "question",
qType: "pollMultiChoice",
question: "",
answers: [{ answer: "", isCorrect: false }],
}}
validate={handleValidations}
/>
);
};

const _ChoiceQuestionFrom = (props) => {
const {
editMode,
values,
submitting,
form: { change: onFormChange },
handleSubmit,
} = props;


const handleListEdit = (e, ind) => {
const { value: answer } = e.currentTarget;
let currentAnswers = [...values.answers];
currentAnswers[ind] = { ...currentAnswers[ind], answer };
onFormChange("answers", currentAnswers);
};

const handleDeleteItem = (ind) => {
let currentAnswers = [...values.answers];
currentAnswers.splice(ind, 1);
onFormChange("answers", currentAnswers);
};

const handleCheckboxChange = (ind) => {
let currentAnswers = [...values.answers];
currentAnswers[ind] = {
...currentAnswers[ind],
isCorrect: !currentAnswers[ind].isCorrect,
};
onFormChange("answers", currentAnswers);
};

const handleQuestionStatementChange = (e) => {
return onFormChange("question", e.target.value);
};
return (
<form onSubmit={handleSubmit}>
<QuestionStatement
value={values.question}
onChange={handleQuestionStatementChange}
outerStyles={styles.questionStatement}
/>
<JunoTypography variant="h2" style={{marginBottom: 10}}><FormattedMessage id="ChoiceQuestionForm.AddChoices" defaultMessage="Add Choices" /></JunoTypography>
{values.answers.map((val, ind) => (
<FieldCheckListItem
key={ind} // change with id
onDelete={() => handleDeleteItem(ind)}
onTextChange={(e) => handleListEdit(e, ind)}
onCheckboxChange={() => handleCheckboxChange(ind)}
textFullWidth={true}
textValue={val.answer}
checked={val.isCorrect}
disabled={editMode}
outerStyle={styles.item}
/>
))}
<div className="flex" style={styles.buttonsContainer}>
<DesignedRoundDashedButton
type="button"
label={
<FormattedMessage
id="ChoiceQuestionForm.AddAnswer"
defaultMessage="Add Answer"
/>
}
onClick={() =>
onFormChange("answers", [
...values.answers,
{ answer: "", isCorrect: false },
])

}
/>

<FieldButton
color="primary"
variant="contained"
submitting={submitting}
type="submit"
style={styles.saveBtn}
>
<FormattedMessage id="Question.Save" defaultMessage="Save" />
</FieldButton>
</div>
</form>
);
};

大家好,我有 React 最终形式的问题,它会在 3 秒后重置值并恢复到初始值......调试了 3 天但没有任何变化......这是代码......任何想法和信息都会受到影响,谢谢

最佳答案

如果到目前为止你还没有解决它,你可以使用 keepDirtyOnReinitialize 属性来解决。正如您在文档中看到的那样,https://final-form.org/docs/react-final-form/types/FormProps#keepdirtyonreinitialize它的目的是不覆盖您到目前为止所做的更改。

但是,发生这种情况的事实可能表明您构建表单组件的方式存在其他问题......但这从您放置在此处的代码中看不到。

关于reactjs - react 最终形式重置为初始值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67402489/

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