[英]Working with react input field says synthetic even when i try and save, and setting value instead of defaultValue is rendering [Object, object]
Disclaimer I am new to developing.免责声明我是开发新手。 I am having trouble when I try and save my changes on my input field I get an error saying "Warning: This synthetic event is reused for performance reasons. If you're seeing this, you're accessing the property
nativeEvent
on a released/nullified synthetic event. This is set to null. If you must keep the original synthetic event around, use event.persist()."我有麻烦时,我会努力保留我对我的输入领域的变化,我得到一个错误说“警告:该合成事件是出于性能的考虑重用。如果你看到这个,你访问属性
nativeEvent
上发布/无效的合成事件。这设置为 null。如果您必须保留原始合成事件,请使用 event.persist()。” Also if I set the "value" instead of the "defaultValue" when I type in the field I get [Object, object].此外,如果我在字段中输入时设置“值”而不是“defaultValue”,我会得到 [Object, object]。
This is the input component:这是输入组件:
const Profile = ({
profile,
mCatalog,
sCatalog,
isEditing,
onChange,
restoreData,
userID,
}) => {
const updateProviderNotes = (event) => {
const { name, value } = event.target;
onChange(name)(value);
}
return (
<Input
type="textarea"
disbaled={false}
name="providerNotes"
value={providerNote}
onChange={updateProviderNotes}
/>
)
const Editor = ({ source, onChange, items, oldItems, name }) => {
return (
<div className="d-flex ml-3">
<div className={styles.bubble}>
<ListEditor
items={items}
oldItems={oldItems || []}
itemListSource={source}
onChange={onChange(name)}
/>
</div>
</div>
);
};
export default Profile;
this is a portion of the parent component这是父组件的一部分
const ProfileData = ({
profile,
mCatalog,
sCatalog,
page,
catalog,
userID,
setProfile,
}) => {
const [editingProfile, setEditingProfile] = useState(false);
const [oldProfile, setOldProfile] = useState(false);
useEffect(() => {
setOldProfile(profile)
}, [])
const handleMProfileCancel = () => {
setProfile(oldProfile)
}
const handleMedicalProfileSave = () => {
console.log("profile", profile)
console.log(typeof profile.medicalProfile.providerNotes)
api.UserRecords.updateMedicalProfile(userID, profile.medicalProfile)
setOldProfile(profile)
}
const updateMedicalProfileDetails = (fieldName) => (value) => {
setProfile({ ...profile, mProfile: {...profile.mProfile, [fieldName]: value }});
};
return (
{page === "medicalProfile" && (
<InfoEditWrapper
data={oldProfile.medicalProfile}
onCancel={handleMedicalProfileCancel}
onSave={handleMedicalProfileSave}
>
<Profile
profile={profile.medicalProfile}
medicalCatalog={medicalCatalog}
surgicalCatalog={surgicalCatalog}
onChange={updateMedicalProfileDetails}
userID={userID}
/>
</InfoEditWrapper>
)}
)
export default ProfileData;
Any advice would be helpful thanks!任何建议都会有所帮助谢谢!
For your warning message, I would refer to this question .对于您的警告信息,我会参考这个问题。 You are basically getting this error because you are using your event in an asynchronous context (updating your state) which isn't allowed.
您基本上会收到此错误,因为您在不允许的异步上下文(更新您的状态)中使用您的事件。 You can avoid this error if you assign your event to a local variable and reference it.
如果将事件分配给局部变量并引用它,则可以避免此错误。
if I set the "value" instead of the "defaultValue" when I type in the field I get [Object, object]
如果我在字段中输入时设置“值”而不是“defaultValue”,我会得到 [Object, object]
Your onChange
event handler will receive a Synthetic event object and your parameter you're passing with it.您的
onChange
事件处理程序将接收一个 Synthetic 事件对象和您与它一起传递的参数。 With your current code you assigned the whole event object as the field value.使用您当前的代码,您将整个事件对象分配为字段值。
Your updateMedicialProfileDetails
method that you are passing as the onChange
prop isn't in your question so I'm using the updateProfileDetails
method as an example:您作为
onChange
道具传递的updateMedicialProfileDetails
方法不在您的问题中,因此我使用updateProfileDetails
方法作为示例:
The following code should work:以下代码应该可以工作:
const updateProfileDetails = (fieldName) => (event) => {
const { value } = event.target;
setProfile({ ...profile, mProfile: {...profile.mProfile, [fieldName]: value }});
};
Your name parameter you are passing with this function is unnecessary since your event object will have the name attribute available, so your code can be updated to the following:您与此函数一起传递的 name 参数是不必要的,因为您的事件对象将具有 name 属性可用,因此您的代码可以更新为以下内容:
<Input
type="textarea"
name="providerNotes"
value={profile.providerNotes}
onChange={onChange}
oldValue={restoreData.providerNotes}
/>
The event handler:事件处理程序:
const updateProfileDetails = (event) => {
const { name, value } = event.target;
setProfile({ ...profile, mProfile: {...profile.mProfile, [name]: value }});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.