AutoSave.js 517 B

123456789101112131415161718192021
  1. import React from "react";
  2. import { FormSpy } from "react-final-form";
  3. const AutoSaveIn = ({ values, save }) => {
  4. const [prevValues, setPrevValues] = React.useState(values);
  5. React.useEffect(() => {
  6. if (JSON.stringify(prevValues) !== JSON.stringify(values)) {
  7. setPrevValues(values);
  8. save(values);
  9. }
  10. }, [values, save, prevValues]);
  11. return null;
  12. };
  13. const AutoSave = (props) => (
  14. <FormSpy {...props} subscription={{ values: true }} component={AutoSaveIn} />
  15. );
  16. export default AutoSave;