Form Field Custom Input Rerender

1 Answer 161 Views
Form Input
Noah
Top achievements
Rank 1
Noah asked on 19 Dec 2023, 03:24 PM
Using version 7.0.2 for all kendo-react dependencies.

I'm trying to use a custom Input component for a form Field component in order to achieve a custom onBlur functionality, which will set a state variable in parent component of the Field.  The following code causes an infinite re-render for the custom input component:


export const User = () => {

    const [existingUser, setExistingUser] = React.useState({});

    const handleUserResponse = (result: any) => {
        if (result.data) {
            setExistingUser(result.data[0]);
        }
    };

    const UserEmailInput = (props: FieldRenderProps) => {
      React.useEffect(() => {
          console.log("UserEmailInput Re-Render");
      }, []);

      const [value, setValue] = React.useState<string>('');
      const emailOnBlur = () => {
        fetch(...).then((response: any) => {
          console.log(response);
          setExistingUser(response.data[0]);
        };
      };

      return (
        <FormMaskedTextBox
            {...props}
            onChange={(e: any) => { setValue(e.value); }}
            onBlur={emailOnBlur}
        />
      );
    };

    return (
        <div style={{ marginTop: '5px', marginBottom: '5px' }}>
            <Card>
                //Form component lives in parent component.
                    <Field
                        name={"email"}
                        label={"E-mail:"}
                        component={UserEmailInput}
                        validator={[
                            requiredValidator("E-mail"),
                            lengthValidator("E-mail", 255),
                            regexValidator("E-mail", emailRegex)
                        ]}
                        id="emailID"
                    />
                </CardBody>
            </Card>
        </div>
    );
};
If I move the UserEmailInput component outside of the GroupUser component, the infinite re-render is resolved, but I lose the ability to set a the state variable in GroupUser.  I could feasibly use a React Context for this, but I'd prefer to use those as sparingly as possible.

Can anyone advise me on this infinite re-render issue, and how I might be able to mitigate it without moving my custom input field outside of the component where the Field is rendered, and where my state variable needs to be set?

1 Answer, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 20 Dec 2023, 06:55 PM

Hello Noah,

When the custom component is defined as an inline function within the main component, this will cause the re-rendering of the Form. As you have noticed, moving the custom component outside of the main component resolves the issue. With that in mind, please move the definition outside of the main component and use React Context for passing references to the variables and the functions that you want to use within it:

Hope this helps.

 

Regards,
Konstantin Dikov
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Kendo family, check out our getting started resources!

Noah
Top achievements
Rank 1
commented on 20 Dec 2023, 07:01 PM

Hi Konstantin,
Thank you for your response.  I'll go ahead and take that action for now.
Having said that, I would strongly prefer to use Contexts as sparingly as possible.  I notice that defining custom GridCellRenderers within the main component works for the KendoReact Grid component, and does not cause this re-render issue.  Is it feasible or at all possible that custom Form Input components could be supported as an inline function of a main component in a future release?  No worries if not, it would just make my use case much more convenient.

Thanks again for your response and guidance!
Konstantin Dikov
Telerik team
commented on 20 Dec 2023, 07:44 PM

Hi Noah,

The same issue with the re-mounting can be observed if a custom cell for the Grid's column is define inline. Any change in the state will cause the Grid to re-mount.

As for a fix, I have to say that this seems to be a limitation for which we can't provide a fix, so using the Context is the only option for passing references to custom cells/editors.

Noah
Top achievements
Rank 1
commented on 20 Dec 2023, 07:46 PM

Oh, I could've sworn that my team had successfully used custom grid cell renderers defined inline to the main component without encountering this issue, but maybe I'm mistaken.

Anyway, I understand.  Thanks for letting me know!
Konstantin Dikov
Telerik team
commented on 22 Dec 2023, 02:46 PM

Hi Noah,

If you are referring to "cellRender" and "rowRender", they can be defined inline, but any custom cell (defined through "cell", "cells", "headerCell", etc.) must be defined as separate components outside of the main one.

Tags
Form Input
Asked by
Noah
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question
or