Skip to content

Commit

Permalink
fix: update usewatch
Browse files Browse the repository at this point in the history
  • Loading branch information
adyfk committed Jun 21, 2023
1 parent de4cd6f commit 5c18462
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
6 changes: 2 additions & 4 deletions src/v2/hooks/useWatch.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {
useContext,
useRef,
} from "react";
import { IForm, IState } from "../logic/createForm";
import { IForm } from "../logic/createForm";
import { FormContext } from "../contexts/FormContext";
import useSubscribeAndCompare from "./useSubscribeAndCompare";

Expand All @@ -15,7 +14,6 @@ export const useWatch = <T>(props: {
}) => {
const { form: formContext } = useContext(FormContext);
const { form = formContext, name } = props as { form: IForm<any>, name: string };
const _state = useRef<IState>(form.getValue(name) || props.defaultValue);

useSubscribeAndCompare({
form,
Expand All @@ -26,7 +24,7 @@ export const useWatch = <T>(props: {
});

return {
state: _state.current as T,
state: form.getValue(name) as T,
form,
};
};
Expand Down
6 changes: 2 additions & 4 deletions src/v2/hooks/useWatchAll.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {
useContext,
useRef,
} from "react";
import { IForm, IState } from "../logic/createForm";
import { IForm } from "../logic/createForm";
import { FormContext } from "../contexts/FormContext";
import useSubscribeAndCompare from "./useSubscribeAndCompare";

Expand All @@ -14,15 +13,14 @@ export const useWatchAll = (props: {
}) => {
const { form: formContext } = useContext(FormContext);
const { form = formContext } = props as { form: IForm<any> };
const _state = useRef<IState["values"]>({} as any);

useSubscribeAndCompare({
form,
subject: "fields",
});

return {
state: _state.current,
state: form.state.values,
form,
};
};
Expand Down

0 comments on commit 5c18462

Please sign in to comment.