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.

Formcontext returns 'null' inside SOME SimpleForm components.

See original GitHub issue

What you were expecting: React-hook-form methods such as useWatch, useFormContext, etc. to work correctly inside a SimpleForm component.

What happened instead: React-hook-form methods inside SOME SimpleForms fail with the error “cannot read properties of null (reading ‘control’)”. In this case, particularly when used in my codebase to wrap inputs on a modal form on a list view’s bulk actions. Elsewhere, such Create and Edit forms, the react-hook-form methods return the expected values.

Steps to reproduce: I’ll provide screenshots below, but perhaps the most frustrating aspect of this bug is that I have been unable to replicate it in a CodeSandbox. Since the problem is likely something to do with my local, I hope this issue can potentially help other RA users who may run into that same problem.

Related code: The below code is a VERY stripped down component which is breaking when used as a Bulk action on a list view in my codebase.

import React from 'react';
import { SimpleForm, TextInput } from 'react-admin';
import { useWatch } from 'react-hook-form';

const TestForm = () => (
    <SimpleForm>
        <TestInputs />
    </SimpleForm>
);

const TestInputs = () => {
    const test = useWatch({ name: 'test' });
    return <TextInput source="test" />;
};

export default TestForm;

Other information: As mentioned above, I really believe that the issue is on my side (especially since I can’t recreate it on CodeSandbox), but my code follows the patterns that previously worked when RA was using react-final-form, and I’m hoping this issue can be used to point me in the right direction for debugging the problem, and hopefully help others who have have run into the same issue or will in the future.

Screenshot for the stack trace below. stacktraceSimpleFormError

Environment

  • React-admin version: 4.0.0.rc-1
  • Last version that did not exhibit the issue (if applicable): 3.19.10
  • React version: 17.0.2
  • Browser: Chrome
  • Stack trace (in case of a JS error):

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
toniwesercommented, May 19, 2022

I was working on a project, where we were using Material UI and included React Admin at a later point.

As React Admin is providing also Material UI dependencies, we did forget to remove the previously used Material UI dependencies. Probably this was causing the error.

After removing the Material UI dependencies, everything was working fine.

1reaction
anon-r-7commented, Apr 19, 2022

Ah nvm this is on me. Using yarn workspaces and thought I had a clean yarn install but I didn’t.

Read more comments on GitHub >

github_iconTop Results From Across the Web

FormContext returns 'null' inside SimpleForm on local
EDIT: As an addendum: When I use the react-admin form component "FormDataConsumer", the form values are correctly passed in the prop "formData".
Read more >
Input Components - React-admin - Marmelab
Input components must be used inside a Form element (e.g. <Form> ... which turns any null or undefined value into an empty string....
Read more >
Advanced Usage - React Hook Form
In this example, there is a simple form without any apparent async code, and the test merely renders the component and tests for...
Read more >
Using useContext and useReducer to Manage a Form's State
It is broken down into several components and each component can be as complex as needed. ... FormContext — A context object initialised...
Read more >
rhodesjason dot com
This is a story about how I accidentally built another form library for React, ... Like so many things in the late 2010s,...
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