React slow with multiple controlled text inputs

后端 未结 8 1383
心在旅途
心在旅途 2021-02-18 13:08

I have a form with multiple text inputs. I have them all set up as controlled inputs. When typing, there is a lag of up to several seconds for the new text to display in the fie

8条回答
  •  不要未来只要你来
    2021-02-18 13:39

    My problem was that my state object was complex and thus causing rendering issues. My solution was to manage the state of the notes in my component, then on blur update the container state.

    const { data, onEdit } = props;
    const { containerNotes } = data;
    
    const [notes, setNotes] = useState('');
    
    useEffect(
      () => {
        setNotes(containerNotes);
      },
      [containerNotes],
    );
    
    const onChangeNotes = () => ({ target: { value } }) => {
      setNotes(value);
    };
    
    const onBlurNotes = (prop) => ({ target: { value } }) => {
      const newData = update(data, {
        [prop]: { $set:  value },
      });
      onEdit(newData);
    };
    
    return (
      
    )
    

提交回复
热议问题