fix(form): move FormItem rule registration out of render phase#7
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 29b59e05d2
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| useEffect(() => { | ||
| ctx.setFieldRules(name, rules) | ||
| }, [ctx, name, rules, dependencyValues]) |
There was a problem hiding this comment.
Register rules synchronously before submit can run
Moving ctx.setFieldRules(name, rules) into useEffect delays rule registration until after the first paint, so validate() can run with an empty rulesMap during the initial mount cycle. This is observable when a consumer submits imperatively on mount (for example from a parent useLayoutEffect/ref call): the first submit bypasses required/pattern checks and can call onSubmit with invalid values, whereas the previous render-phase registration made rules available immediately.
Useful? React with 👍 / 👎.
Motivation
void ctx.values[dep]read-to-trigger approach with an explicit derived dependency tracking so effect dependencies are predictable.Description
packages/ui/src/components/form/Form.tsxto importuseEffectand movedctx.setFieldRules(name, rules)from the render body into auseEffectthat depends onctx,name,rules, and a deriveddependencyValues.dependencies.forEach(dep => void ctx.values[dep])withconst dependencyValues = useMemo(() => dependencies.map(dep => ctx.values[dep]), [dependencies, ctx.values])and included it in the effect dependencies.rulesMapduring render and re-registers rules only when the context, name, rules, or actual dependency-derived values change.Testing
npm run lint:ui, which completed successfully.npm run typecheck:ui, which completed successfully.Codex Task