简体   繁体   English

即使我尝试保存,使用 react 输入字段也会显示合成,并且设置值而不是 defaultValue 正在呈现 [Object, object]

[英]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.

相关问题 React - 将输入道具从 defaultValue 设置为 value 触发无限循环 - React - setting input prop from defaultValue to value triggers infinite loop javascript object 字段输入值 [object Object] 代替字符串 Reactjs - javascript object field input value [object Object] instead of string Reactjs javascript object 字段设置输入值[object Object]而不是float - javascript object field set input value [object Object] instead of float 将输入字段值设置为空值 state object 后,现在我无法在 UI 的输入字段中键入任何内容。 我正在使用反应 js - after setting the input field value to a state object with an empty value, now I can't type anything in the input field in the UI. i am using react js React 输入 defaultValue 重新渲染 - React input defaultValue re-rendering 按值而不是引用设置 object - Setting object by value instead of reference 在输入字段中将 defaultValue 和 value 一起反应 - React defaultValue and value together in input fields 设置 Object = [ ] 时返回 Not Rendering,设置 Object = {} 时有效 - Return Not Rendering when setting Object = [ ], works when setting Object = {} 当我添加两件事时,它说[object Object] [object Object] - It says [object Object][object Object] when I add two things Angular $scope 对象显示 $scope.data 但当我尝试使用它时,显示未定义 - Angular $scope object showing $scope.data but when I try to use it, the says undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM