Skip to content
Discussion options

You must be logged in to vote

I've ended up creating AutoSubmit component that subscribes to form's state. This way only this component rerenders.

const AutoSubmit = () => {
  const form = useFormContext();

  const [canSubmit, someVar] = useStore(form.store, (state) => [
    state.canSubmit,
    state.values.someVar
  ]);

  useEffect(() => {
    const autoSubmitForm = () => {
      if (!canSubmit) { return; }

      if (/* Logic that uses someVar */) { return; }

      form.handleSubmit();
    };

    autoSubmitForm();

    // NOTE: Don't add form to the dependencies
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [
    canSubmit,
    someVar,
  ]);

  return <></>;
};

This component can be dropped …

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@yuzhakovvv
Comment options

@LeCarbonator
Comment options

@yuzhakovvv
Comment options

@yuzhakovvv
Comment options

Answer selected by yuzhakovvv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants