Skip to content

Commit

Permalink
fix(react): fix useFormEffects not support StrictMode #1491
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed May 31, 2021
1 parent 047c98a commit 0198b0c
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions packages/react/src/hooks/useFormEffects.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { useEffect, useMemo, useRef } from 'react'
import { useLayoutEffect, useMemo } from 'react'
import { uid } from '@formily/shared'
import { useForm } from './useForm'

export const useFormEffects = (
effects?: (form: Formily.Core.Models.Form) => void
) => {
const ref = useRef(null)
const form = useForm()
ref.current = useMemo(() => {
const ref = useMemo(() => {
const id = uid()
form.addEffects(id, effects)
return id
const request = setTimeout(() => {
form.removeEffects(id)
}, 100)
return { id, request }
}, [])
useEffect(() => {
useLayoutEffect(() => {
clearTimeout(ref.request)
return () => {
form.removeEffects(ref.current)
form.removeEffects(ref.id)
}
}, [])
}

0 comments on commit 0198b0c

Please sign in to comment.