Error message update or error type change should trigger reRender
See original GitHub issueError message update or error type change should trigger reRender
rules: {
validate: data => {
if (data === "mm") {
return true;
}
if (data !== "11") {
return "something error";
}
return "error message";
},
required:true
}
import { Form, Input } from "antd";
import React, { useEffect } from "react";
import { FormItemProps } from "antd/lib/form";
import { FieldError } from "react-hook-form/dist/types";
import { InputProps } from "antd/lib/input";
import { useFormContext } from "react-hook-form";
import { FiledType } from "./Demo";
import _ from "lodash";
interface InputItemProps {
form?: FormItemProps;
filed: FiledType;
input?: InputProps;
}
export const MyInput = (props: InputItemProps) => {
const { register, setValue, errors } = useFormContext();
const { form = {}, filed } = props;
const { name, rules } = filed;
useEffect(() => {
register({ name }, rules);
});
const error = (errors[name] || {}) as FieldError;
return (
<Form.Item
{...form}
label={form.label || name}
help={error.message}
validateStatus={_.isEmpty(error) ? "success" : "error"}
>
<Input
onChange={e => {
setValue(name, e.target.value, true);
}}
/>
</Form.Item>
);
};
Even the error message change ,it’s never trigger reRender.
const renderBaseOnError = useCallback(
(
name: FieldName<FormValues>,
error: FieldErrors<FormValues>,
shouldRender = false,
) => {
let reRender = shouldRender;
if (isEmptyObject(error)) {
if (fieldsWithValidationRef.current.has(name) || validationSchema) {
validFieldsRef.current.add(name);
reRender = reRender || errorsRef.current[name];
}
delete errorsRef.current[name];
} else {
validFieldsRef.current.delete(name);
reRender = reRender || !errorsRef.current[name];
}
errorsRef.current = validationSchema
? schemaErrorsRef.current
: combineErrorsRef(error);
if (reRender) {
render({});
}
},
[validationSchema],
);
Design so or A bug?
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
How and when to force a React component to re-render
React automatically re-renders components, but what happens when a component is not updating as expected? Find out what you can do here.
Read more >Why is useState not triggering re-render? - Stack Overflow
Based on this code, it seems that the input should contain the number 0 to start, and any time it is changed, the...
Read more >React doesn't always trigger a re-render on setState
If the value doesn't change, React will not trigger a re-render. ... Once it changes, React will immediately receive the update and trigger...
Read more >When does React re-render components? - Felix Gerschau
Changing the state means that React triggers an update when we call the setState function (in React hooks, you would use useState )....
Read more >Why React Re-Renders - Josh W Comeau
If that user state variable changes, a re-render will occur, and GreetUser will generate a new snapshot, rather than relying on a stale...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks @bluebill1049 .Next time I will supply a codesandbox for issue report.
that will be great @Chastrlove, we all want this lib to be solid and work for everyone 👍