question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error message update or error type change should trigger reRender

See original GitHub issue

Error 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:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Chastrlovecommented, Nov 15, 2019

Thanks @bluebill1049 .Next time I will supply a codesandbox for issue report.

0reactions
bluebill1049commented, Nov 15, 2019

that will be great @Chastrlove, we all want this lib to be solid and work for everyone 👍

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found